Thread: Extracting Whitespaces

  1. #1
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050

    Extracting Whitespaces

    Okay, I've spent several hours looking and trying to figure out a way to extract whitespaces from a .txt file. I'm not sure if I'm explaining this correctly or not so far, so I'll give an example.

    My .txt file reads like this

    12
    GameDev
    15.45
    L
    This is really awesome!

    Yep, this came from a tutorail on gamedev. When I take in "This is really awesome!" into my program and display it I want all of it to display...not just "This". For some reason what I'm doing (using getline() ) isn't displaying anything at all when I cout that line. Well, here is my code, and if anybody could tell me what I'm doing wrong that would be great. I'm sure it isn't nearly as hard as I'm making it out to be, but I can't figure it out for some reason.

    Code:
    #include <fstream.h>  //used for 
    #include <conio.h>
    
    void main()
    {
         //lets represent the file as fin "file in"     
         ifstream fin("E:\\Documents and Settings\\Administrator\\Desktop\\bla\\test.txt");
         //where the text file is located at ^
              
         int number; // first line in .txt
         float real;  // second line in .txt
         char letter, word[8]; //third and fourth line in .txt
         char sentence[101];   //fourth line in .txt
         // ^ this is the line of "this is totally awesome!"
         
         fin >> number >> word >> real >> letter;  //to receive all of the files
         // ^ this part works fine when displaying
         
         fin.getline(sentence, 100);  //get up to 100 characters from the file
         /*that should include the whitespaces too
         instead nothing is displayed at all when trying to cout */ sentence
         /*if i use "fin >> sentence" like the rest
         it only displays "this" when COUTed
         which should be the case when using fin (cin)
         i've tried a few other things but none of them seem to work for me */
    
                 
        cout << number << endl; // displays fine
        cout << word << endl;  // displays fine
        cout << real << endl;   // displays fine
        cout << letter << endl;  // displays fine      
        cout << sentence << endl; // displays fine
    
    
          getch();  // waits for user to hit "any key" then terminates program
    }
    Btw, I'm using DevC++ 5beta4.9.2.0, and it being in beta is not the reason for it not working because it doesn't work with DevC++4.01 either.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    fin.getline(sentence, 100);

    Should read

    fin.getline(sentence, 100, '\n');

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    fin.getline(sentence, 100, '\n');
    Why oh why couldn't that *** *** tutorial just say that in the first place.

    Well, thank you very much for that, because I was starting to get (actually I was) very frustrated. Just to let you know, though, it was '/n' not '\n'...sorry but I had to point it out to you.

    Now I can go finish my maze game, but first I wanted to get down some key factors in I/O. That way I can save 'record' files and other files for the game.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    file saves

    If you save to a file scores or something like this:

    bob 100
    nancy 33
    gozilla 66

    Then its quite eazy to tamper with the save, you might consider doing some very simple encription...
    Chance favors the prepared mind.

    Vis. C++ 6.0

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Hmm? I've always used '\n'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whitespaces
    By happyclown in forum C Programming
    Replies: 2
    Last Post: 01-06-2009, 10:33 PM
  2. sscanf & whitespaces
    By hannibar in forum C Programming
    Replies: 1
    Last Post: 05-10-2006, 09:06 AM
  3. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:17 PM
  4. Extracting Rars
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2002, 02:53 AM