Results 1 to 13 of 13

Thread: Read from a file to a string

  1. #1

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    Read from a file to a string

    Hello I am new to C++ and i have the new visual c++ .net

    i was woundering if someone could let me know a simple way to read from a *.txt file and put it into a string or a string array one line at a time. if someone could give me some sample code that would be great!

    Thanks ahead of time

  2. #2
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
            ifstream fin;
            string Buffer;
    
            fin.open("FileName.txt");
    
            if(fin.fail())
           {
                  cout << "Error Opening File\n"; //if the file is not found then display error and exit
                  return 0;
           }
           
          while(getline(fin,Buffer))
          {
                  cout << Buffer << "\n";    //reads through the file untill the end displaying everyline
          }
    	  return 0;
    }
    Make sure that in the folder where u save tihs code is the txt file, just change where it says "FileName.txt" to the name of the file
    This code will read through the file collecting every line and printing it.

    Hope this helps

    BTW im not sure if this will work with VC++.Net since i don't have it, but im guessing it should
    Death is always smiling down on us, the only thing we can do is smile back

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Read from a file to a string

    Originally posted by Buy2easy.com
    Hello I am new to C++ and i have the new visual c++ .net

    i was woundering if someone could let me know a simple way to read from a *.txt file and put it into a string or a string array one line at a time. if someone could give me some sample code that would be great!

    Thanks ahead of time
    Do you search at all before you post? Check out the FAQ or do a search first. You'll be surprised at what you find, especially with a question like this.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Lively Member
    Join Date
    May 2002
    Location
    Oregon
    Posts
    64
    First off let me say I'm not trying to be rude or step on anyone's toes. But I've noticed this disturbing trend among forums lately where questions are repeatedly getting answered with "Do a search, read the FAQ". Most of these have rather unpleasant tones. Then when people do searches they end up with the first page being "do a search, read the FAQ" replies. Fortunately I haven't noticed it as bad here but just wanted to bring up the topic.

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Arawn
    First off let me say I'm not trying to be rude or step on anyone's toes. But I've noticed this disturbing trend among forums lately where questions are repeatedly getting answered with "Do a search, read the FAQ". Most of these have rather unpleasant tones. Then when people do searches they end up with the first page being "do a search, read the FAQ" replies. Fortunately I haven't noticed it as bad here but just wanted to bring up the topic.
    Seeing someone ask "How do I make a message box?!" everyday gets a little monotonous.

    The reason why I said what I said was because, about two posts down, I asked a question pertaining to nearly the same thing. Which pretty much goes to prove that he/she didn't look very much before they posted.

    I'm sorry if I offended you. Perhaps you could ignore me in the future? Just for reference, there is an ignore option in your User CP. Feel free to use it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Originally posted by RabidChimp
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
            ifstream fin;
            string Buffer;
    
            fin.open("FileName.txt");
    
            if(fin.fail())
           {
                  cout << "Error Opening File\n"; //if the file is not found then display error and exit
                  return 0;
           }
           
          while(getline(fin,Buffer))
          {
                  cout << Buffer << "\n";    //reads through the file untill the end displaying everyline
          }
      fin.close();
    	  return 0;
    }
    In general it's a good idea to close the filestream also, especially when you have multiple files open.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    Thanks for your reply it helped me out a lot.......also how do i make a message box? Just kidding

  8. #8
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    my bad i forgot to close it, *slaps himself*
    Death is always smiling down on us, the only thing we can do is smile back

  9. #9

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200

    small problem

    i have a small problem w/ this code...i used it as is to test it and i put a file in the directory of the *.exe file w/ the name filename.txt i even matched the text to exactly match what was posted i think it was FileName.txt. But it keeps giving me the error message like it can't read the file.....What could be wrong?

    Thanks!

  10. #10
    Member
    Join Date
    Dec 2002
    Location
    Miami,FL
    Posts
    34
    What was the actuall error? Also Try putting the txt file where the .cpp file instead of in the Debug folder where the exe is.
    Death is always smiling down on us, the only thing we can do is smile back

  11. #11

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    the error says that the file cannot be opened...not an error message but the build in message that displays when the file can not be opened or is not found (build into your code example)

    why would it need to be in the dir w/ the .cpp file? just woundering? I will try it when i get home. and i wil get back w/U

  12. #12

    Thread Starter
    Addicted Member Buy2easy.com's Avatar
    Join Date
    Jul 2002
    Posts
    200
    that fixed it! what is up w/ that why did it not need to be in the dir w/ the .exe?

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When you run it direct from VC++, it changes the working directory to your project directory, *not* the output directory. This is so that you can use the same files with both debug and release versions.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width