Including spaces with an ifstream...

This is a discussion on Including spaces with an ifstream... within the C++ Programming forums, part of the General Programming Boards category; How might I use Code: ifstream infile; infile.open("filename.txt",ios:in); I'm trying to get the infile to read spaces... so... infile.unsetf(ios::skipws); That ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    ofstream/ifstream

    How might I use
    Code:
    ifstream infile;
    infile.open("filename.txt",ios:in);
    I'm trying to get the infile to read spaces... so...

    infile.unsetf(ios::skipws);

    That seems to just make it pause indefinately sometimes it crases with a runtime error... whats wrong with it?

    Code:
    	cout << "What message do you want to decrypt?\n";
    	infile.open("unencrypted.txt",ios::in);
    	infile.unsetf(ios::skipws);
    	
    	strcpy(sInput,"\0");
    
    	while (!infile.eof())
    	{
    		infile >> sBuffer;
    		strcat(sInput,sBuffer);
    	}	
    	infile.close();
    Last edited by Trauts; 10-29-2002 at 07:40 PM.

  2. #2
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    What code are you using to read in the file? If you use read() then i don't think that will happen.

    ~Inuquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    hmm... edited the code. does that help?
    Last edited by Trauts; 10-29-2002 at 07:14 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I don't know how to use read().

    Basically, I want to copy the entire contents of the text file including all spaces, etc, into the character array (string) sInput.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Sounds like a buffer overflow.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    hmm... that doesn't help me any ... there aren't very many spaces in the file, and it works perfectly if that one line isn't in it.

    Perfectly if you don't count that this post ends up looking like this:

    hmm...Idon'tknow...therearen'tverymanyspacesinthef ile,anditworksperfectlyifthatonelineisn'tinit.Perf ectlyifyoudon'tcountthatthispostendsuplookingliket his:
    Last edited by Trauts; 10-29-2002 at 07:15 PM.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Code:
    	char sInput[1024];
    	char sBuffer[1024];

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Come on... this has got to be simple and you know it!

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    FSTREAMTEST caused an invalid page fault in
    module KERNEL32.DLL at 017f:bff87ede.
    Registers:
    EAX=c00309c4 CS=017f EIP=bff87ede EFLGS=00010212
    EBX=0067fe28 SS=0187 ESP=0057ff94 EBP=00580000
    ECX=005801b4 DS=0187 ESI=8180660c FS=5737
    EDX=bff76855 ES=0187 EDI=005801dc GS=0000
    Bytes at CS:EIP:
    53 56 57 8b 30 83 7d 10 01 8b 4e 38 89 4d f8 75
    Stack dump:

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Including spaces with an ifstream...

    I'm trying to use an ifstream and copy all the content into a character array, but it crashes the program if the line is in the code.

    http://cboard.cprogramming.com/showt...threadid=27536 has a more in depth thing, but that is going down and down on the list and you aren't supposed to bump it...

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Like people said in the thread... use read() or getline()

    char pszBuffer[256];
    fileIn.getline(pszBuffer, 256, ' '); // read up to a space OR 256 chars;

    or

    fileIn.read(pszBuffer, 256); // read 256 characters

    or you could try using setf(ios::skipws) instead of usetf(ios::skipws)

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    All right, I got that working, but how do I make it so that the strings will fit the contents of the text file no matter what? I.e. no fixed array size?

  13. #13
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Just keep "fin >> next; sOutput += next; sOutput += ' ';" until fin.eof() is true. Keep in mond, sOutput has to be a string (the STL class). This would apso eliminate overflow, and allow better operator support (as you see).

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  14. #14
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35
    Easiest way would be to use the STL string class.
    Otherwise, open the file, read every character one by one using the ifstream get function, and increment a counter at each one.
    then you have a counter telling you how long the file is.
    Make an array that long and read it as suggested before.
    - Well that's my 4c Australian.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while (!infile.eof())
    This is the wrong way to control your loop.

    If you want to load the complete file into memory in one go, you could try read(), as already stated. There's a small example, of sorts, here. Basically, work out the file size, create an array big enough to hold that many bytes, and read() the file in.

    And don't bump your thread again. If you want to change a previous post, use the edit button.

    [edit]And now I've just merged your threads, having replied on the old one Don't do this again, or they may end up being deleted. Any problems/questions, feel free to PM me and ask.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. fgets - user input including spaces
    By MethodMan in forum C Programming
    Replies: 24
    Last Post: 03-12-2004, 07:36 PM
  5. why can't ifstream read spaces or enters
    By bobish in forum C++ Programming
    Replies: 11
    Last Post: 06-13-2002, 05:23 PM

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