File Reading Help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: File Reading Help

  1. #1
    Join Date
    Apr 2009
    Posts
    82

    File Reading Help

    My objective is to store the desired date and time.
    In this case, I wish to stop reading when encounter the next blank line. Meaning I only wish to store the date and time - 20110129 18:47:01.
    However, by using code below I will get the latest time stamp - 20110129 19:05:09 02.
    Is there any better way to break from the while loop when encounter the blank line in between?

    Sample file content:
    Time_Stamp Value
    20110129 18:21:12 01
    20110129 18:40:59 02
    20110129 18:47:01 02

    20110129 18:59:06 02
    20110129 19:05:09 02

    Sample code:
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	char inLine[255]="", rDate[30]="", rTime[30]="";
    	FILE *fptr;
    
    	if(( fptr = fopen( "TimeStamp.txt", "r" )) != NULL )
    	{
    		fgets (inLine, 255, fptr);
    		
    		while (!feof (fptr))
    		{	
    			fgets (inLine, 255, fptr);
    			sscanf (inLine, "%s %s", rDate, rTime );
    
    		}
    	}
    
    	return 0;
    }
    See attached image for more info.

    Hope you all can advise.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: File Reading Help

    You aren't even attempting to check for the blank line in your code, so what do you expect?

    I'd say one easy way to check for that condition is checking the value returned by sscanf(). It returns the number of fields it successfully parsed which will be 2 for the lines actually containing a timestamp. The return value will be something else for the blank line.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: File Reading Help

    I would like to add that you are not processing the first line at all.
    Also, you are not using feof() correctly: EOF condition is not set until AFTER you attempt to read past the end of file.
    Itís better (easier AND correct) to simply check return value of fgets():
    Code:
    while(fgets(inLine, 255, fptr))
    {
        sscanf (inLine, "%s %s", rDate, rTime);
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: File Reading Help

    Quote Originally Posted by VladimirF View Post
    I would like to add that you are not processing the first line at all.
    I initially wondered about that too, but it's because the first line of the input file contains the column headers.

    Also, you are not using feof() correctly: EOF condition is not set until AFTER you attempt to read past the end of file.
    Itís better (easier AND correct) to simply check return value of fgets():
    Code:
    while(fgets(inLine, 255, fptr))
    {
        sscanf (inLine, "%s %s", rDate, rTime);
    }
    Oh, yes. However, the posted code in conjunction with the attached demo input file doesn't expose apparent problems resulting from that because the demo file's last line is lacking the line terminator. It probably would not work if it had one. OTOH, that way the code you posted would probably ignore the last line of that particular file, but that's just exposition of the faulty format of the input file...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Apr 2009
    Posts
    82

    Re: File Reading Help

    There is no need to process the first line as it's the column header.

    Thanks Eri523 and VladimirF for helping.
    I guess I will change my code as follow so that it will stop reading when encounter blank line whereby sscanf return value is not 2.

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	char inLine[255]="", rDate[30]="", rTime[30]="";
    	FILE *fptr;
    
    	if(( fptr = fopen( "TimeStamp.txt", "r" )) != NULL )
    	{
    		fgets (inLine, 255, fptr);
    		
    		while (!feof (fptr))
    		{	
    			fgets (inLine, 255, fptr) ;
    			if(sscanf (inLine, "%s %s", rDate, rTime )!=2)
    			{
    				printf("Encounter blank line!");
    				break;
    			}
    			
    
    		}
    
    		printf("Date=%s, Time=%s",rDate,rTime );
    
    	}
    
    	return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center