ifstream problem

This is a discussion on ifstream problem within the C++ Programming forums, part of the General Programming Boards category; Hi. If you read a file and a line like this: Code: ifstream A ( "Startup.txt" ); if ( A.is_open() ...

  1. #1
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133

    ifstream problem

    Hi.
    If you read a file and a line like this:
    Code:
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline( ThePassword, 100 );//The first line of the file is the string ThePassword,
    //it automaticly after reading make's some kind of endl; after reading that line
    //But now I want that he reads the value of int Check1;
    //If I enter A.getline( Check1 ); it gives me a compile error, proberly getline is for strings not for int;
    //So I thought of A>> Check1; but the problem is that if you read another line
    //it wont read the line under A>> Check1; but the same line because he doesn't do an endl; after reading
    //So is there some skipline(); thing or >>endl; thing, that after reading A>> Check1; that he goes to the next
    //line and he read's that next line? Any help is welcome, here is the whole read/write code so far:
    
        ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline( ThePassword, 100 );
        A>> Check1;
        A.getline( TheConfirmCode, 100 );
        A>> Check2;
        A.close();
        }//This doesn't work :(
    
        ofstream B ( "Startup.txt" );
        B<< ThePassword<<endl;
        B<< Check1<<endl;
        B<< TheConfirmCode<<endl;
        B<< Check2<<endl;
        B.close();//This does work because of B<< Check1<<endl;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You can call getline again with a dummy string variable to get the newline after the int. You can also just call get() if you are sure that there are no more characters after your int before the end of the line (as it appears in this case). Finally, you could use ignore(), with no parameters it will behave like get, if you put in parameters you can make it behave like getline.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    //it automaticly after reading make's some kind of endl; after reading that line
    At the end of the line in the file, when you were typing in the data, you hit Return to type in the next line. That means there is an invisible line feed character at the end of the line. When you read from the file, C++ also reads in that invisible line feed character.

    //If I enter A.getline( Check1 ); it gives me a compile error, proberly getline is for strings not for int;
    That is correct.

    //So I thought of A>> Check1; but the problem is that if you read another line
    //it wont read the line under A>> Check1; but the same line because he doesn't do an endl; after reading
    There is a marker in your file that marks the spot where you stopped reading, and the next read starts from there. Furthermore, the >> operator is programmed to read input in a certain way. The way it works is that it skips any leading whitespace(spaces, tabs, newlines) and stops reading when it encounters any whitespace. So, if your file looks like this:

    sometext 1343432
    othertext 3423423

    and you read in "sometext", then the file marker will be at the end of "sometext". Then if you do:

    A>>check1;

    the number at the end of the first line will be read in, and the file marker will be set at the end of the number. If you read again, the second line will be read. To prove it, try this program:
    Code:
    data.txt:
    
    1111 2222 3333
    4444 5555 6666
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    int main()
    {
    	ifstream in("C:\\TestData\\data.txt");
    	if(!in)
    	{
    		cout<<"couldn't open file"<<endl;
    		return 1;
    	}
    	
    	int check1 = 0;
    	while(in >> check1)
    	{
    		cout<<check1<<endl;
    	}
    	
    	return 0;
    }
    You can also use the >> operator to read in text. You just have to read the input into the proper variable type. For the data above, you can declare a string variable and an int variable and read in the data like this:
    Code:
    string theText;
    string check1 = 0;
    
    while(in>>theText>>check1)
    {
    	cout<<theText<<" "<<check1<<endl;
    }
    However, you cannot read in spaces with the >> operator, so if your text is more than one word, it won't work. If there are the same number of words on each line, then you can read in, say 3 words, and then read in the number:

    in>>firstWord>>secondWord>>thirdWord>>check1;

    If there are a different number of words on each line, then you should consider rearranging your input file. You could put all the text on one line and then the number on the next line.
    Last edited by 7stud; 08-15-2005 at 02:49 PM.

  4. #4
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Do you mean like:
    Code:
    1.  ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline( ThePassword, 100 );//1st line
        A>> Check1;//2nd line
        A.getline( Dummy, 100 );//2nd line
        A.getline( TheConfirmCode, 100 );//3rd line
        A>> Check2;//4th line
        A.close();
        }//So, is this correct?
    
    2.  ifstream A ( "Startup.txt" );
        
        if ( A.is_open() ){
        A.getline( ThePassword, 100 );
        A.get( Check1 );//Note this can have 1 till 100 as value ( 1,2,3,4... 54,55,56...100,etc.)
        A.getline( TheConfirmCode, 100 );
        A.get( Check2 );//Note this can have 1 till 100 as value ( 1,2,3,4... 54,55,56...100,etc.)
        A.close();
        }//Is this correct?
    3. Sorry I don't get the last one. Are 1 and 2 correct?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The first one is correct. It should work.

    The second one is not what I meant. I meant to add a.get() after a >> Check1;. This will get the newline from the end of the previous line so that the next call to getline will start on the next line.

    The goal is for you to understand what each one does and why they would work.

  6. #6
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    What kind of influence (I hope I wrote that correct) does get() has(except going to the next line)? like here:
    Code:
        if ( A.is_open() ){
        A.getline( ThePassword, 100 );
        A>> Check1;
        A.get();
        A.getline( TheConfirmCode, 100 );
        A>> Check2;
        A.close();
        }
    Thanks for your help, what would you use(1, 2 or 3)?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    get() will get a single character. In this case, it will get the newline character that comes after the integer stored in Check1. It has no other influence.

    I would use 3 or 2 (3 is the same as 2 except you put ignore() instead of get()-- ignore() is slightly more meaningful since the goal is to ignore a single character).

    I would also be using the C++ string class, which doesn't have anything to do with your current question, I just thought I'd throw that out there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

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