read from file to array. about fin.eof() \n EOF...

This is a discussion on read from file to array. about fin.eof() \n EOF... within the C++ Programming forums, part of the General Programming Boards category; source file: 1,2 2,5 3,7 4,9 5,11 Code: int readin(_tag_xandy data[100], ifstream & fin, int & n)// read data from ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    Question read from file to array. about fin.eof() \n EOF...

    source file:
    1,2
    2,5
    3,7
    4,9
    5,11


    Code:
    int readin(_tag_xandy data[100], ifstream & fin,  int & n)// read data from file function
    {
    	
    	char tmpchar;
      
    	while (!fin.eof())
    	{
    		
    		fin>>data[n].x>>tmpchar>>data[n].y;
    		
            cout<<data[n].x<<','<<data[n].y<<endl;
    			++n;		 
    		//cout << "what is n??"<<n<<endl; 
    		   //for dubugging
    	}
        
    	
    	//if(data[n].x==NULL||data[n].y==NULL)		
    		//return n-1;
    	//else 
    		return n ;
    	//cout<<"after n=n-1...~~n="<<n<<endl;
    }
    output:

    1,3
    2,5
    3,7
    4,9
    5,11
    0,0

    This function is to read from a text file, the output is generated of the struct of arrays in another function.

    As you can see, the source file dont contain that group of number 0,0

    which appear in the output.

    Rest of the codes are okay, I think something cause the difference.

    Could u help me out?How to change the codes???Thanks.



    The red part may the causes. The cursor in the source file is not just at the end of the file.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    read FAQ why you should not use feof to control loop

    It should be like this:

    Code:
    while(fin>>data[n].x>>tmpchar>>data[n].y)
    {
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    2,5
    4,9
    5,11


    Here is the new result, still ....not right

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    i hope it works and reger to the FAQ,

    i still not understand why for this case now work well

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by hth373737 View Post
    2,5
    4,9
    5,11


    Here is the new result, still ....not right
    it seems you have leaved the fin>> line inside the loop as well

    you need only one read per iteration
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by vart View Post
    it seems you have leaved the fin>> line inside the loop as well

    you need only one read per iteration
    Ye,,,

    that is it.

    but could you explain why this happen? how the statement work?

    i cannot understand that statement, as well as the FAQ

    Thanks!

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by vart View Post
    it seems you have leaved the fin>> line inside the loop as well

    you need only one read per iteration
    what is the terminate condition of that while statement?

    how that happen?

    why if i add that fin>> line twice, number 1 and 3 group are to be eaten?

    confusing

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    the eof flag is set as a result of the failed operation.

    it means - BEFORE failure it is always false.

    AFTER failaure - you will chekc it on the next iteration only.

    Instead you should check the return value of th eread operation - and if it fails - do not enter the loop

    for istreams
    if(istr >> x)

    is a standard way to check that the read was successful
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by vart View Post
    the eof flag is set as a result of the failed operation.

    it means - BEFORE failure it is always false.

    AFTER failaure - you will chekc it on the next iteration only.

    Instead you should check the return value of th eread operation - and if it fails - do not enter the loop

    for istreams
    if(istr >> x)

    is a standard way to check that the read was successful
    the compiler can only tell the TRUE or FALSE ,right?

    I never know about the Falure.

    why if cin>> line is in the block,also, cause the strange result?

    thanks

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by hth373737 View Post
    why if cin>> line is in the block,also, cause the strange result?

    thanks
    Because you have read 2 lines and processed only the second one i neach iteration
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 09:51 AM

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