Reading a whole Text file.

This is a discussion on Reading a whole Text file. within the C++ Programming forums, part of the General Programming Boards category; Hello, i have made an encryption program, but im wondering how to read a whole text file and save it ...

  1. #1
    kevinawad
    Guest

    Reading a whole Text file.

    Hello, i have made an encryption program, but im wondering how to read a whole text file and save it to a char.

    I'm using eof to actually read until the end of the file.

    But, it doesn't actually save the whole file to the char.

    So actually, it will only read one line, and not the whole file.

    I hope you can help me, thank you.
    Last edited by kevinawad; 08-04-2008 at 10:43 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    If your text file is longer than one character long, then of course it won't fit in a char.

    Anyway, is there a reason you need the whole file at once? (It's not unheard of to process N characters at a time, for some reasonably large N.) If so, then you'll have to figure out how large the file is, make sure you have enough memory space somewhere, and then read into that memory.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    508
    Save it to a char? Theres no way you could save a whole text file to a single character.

  4. #4
    kevinawad
    Guest
    I know, hehe, i actually need to read the whole file to encrypt the whole file.

    All im encrypting is 1 line.

    Not in 1 char. In a char array. I'm actually saving a complete line. But i just need the code to read a whole file.


    I'm not really familiar with Fstream.

    You can check my other topic: Ageofempire text game. I'm actually not a begginer in c++. It's just fstream. I'm still learning it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Quote Originally Posted by kevinawad View Post
    I know, hehe, i actually need to read the whole file to encrypt the whole file.

    All im encrypting is 1 line.

    Not in 1 char. In a char array. I'm actually saving a complete line. But i just need the code to read a whole file.


    I'm not really familiar with Fstream.

    You can check my other topic: Ageofempire text game. I'm actually not a begginer in c++. It's just fstream. I'm still learning it.
    If you have a char array, then you can only save N-1 characters, where N is the size of the array that you declared.

    Maybe I wasn't clear with my statement: if you're only encrypting one line, why do you care about the rest of the file? (I mean, why do you need to keep it around? Why not just write it to the new file right away and be done with it?)

    But again, if you need the whole file, then you need to first find out how large the file is, so that you can create enough chars to hold it all.

  6. #6
    kevinawad
    Guest
    Don't worry about the char...

    I'm just asking how to read a whole file , that's all.

    Thank you, and i hope you can help me.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Quote Originally Posted by kevinawad View Post
    Don't worry about the char...

    I'm just asking how to read a whole file , that's all.

    Thank you, and i hope you can help me.
    But ... your first post says you know how to read a whole file. If for some reason you don't, it looks like this:
    Code:
    while (inFile >> foo);
    assuming you're willing to go word by word. If you wanted to go lines at a time, you could use getline instead.

  8. #8
    kevinawad
    Guest
    char str[5000];

    while(PhpFile >> str);

    Doesnt work.

    Even if i write

    while(PhpFile >> str[5000]); it will print many chars.


  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Of course it works. As mentioned it will read one word at a time.

    If you want more specific help, maybe you should ask a specific question.

  10. #10
    kevinawad
    Guest
    Nevermind, i found out how...

    Code:
     
    string All;
     
    while(!PhpFile.eof())
    {
    PhpFile.getline(str, 5000);
    All = All + str;
    }


    Then, str will be == to All.

    So actually, it will save to a string, then resave to a char. Thank you.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    13,007
    Quote Originally Posted by kevinawad View Post
    So actually, it will save to a string, then resave to a char.
    [/SIZE]
    As long as you're aware that this sentence is not in any way true, then we're fine.

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    111
    Quote Originally Posted by kevinawad View Post
    Nevermind, i found out how...

    Code:
     
    string All;
     
    while(!PhpFile.eof())
    {
    PhpFile.getline(str, 5000);
    All = All + str;
    }


    Then, str will be == to All.

    So actually, it will save to a string, then resave to a char. Thank you.
    Yeah, what tabstop says about your last two lines there.

    Also, don't use eof() like you are for testing before a read. Just put the getline call into your while loop condition. eof() is used after a read fails to help determine what caused the failure.

    Lastly, you are throwing away newlines. getline() omits them. You will have to put those back in when you append the line to All.

    However, you can avoid all of this crap and fuss by just reading straight into the string with istream_iterator. You won't even need the character array or any loops to do it.

    Code:
    #include <iomanip>     // for noskipws, to keep whitespace and newlines
    #include <iterator>    // for istream_iterator
    ...
    PhpFile >> noskipws;     
    string All(istream_iterator<char>(PhpFile), istream_iterator<char>());
    (not tested)...and that should slurp the whole file into the string All. Hocus Pocus!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 02:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21