Thread: File I/0

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    24

    File I/0

    So, I have posted here before and you have all been a great help. My problem is that I am still a really bad programmer.

    I am attempting to read in data from a text file and have a couple of questions.

    1) How do I read in data, but exclude the quotation marks and commas?
    2) How do I add an eof statement to stop my endless while loop.

    THANKS!!

    Sample of my text file:
    Code:
    "#2",,0,130.00,170.00
    "#2ACSR",,0,160.00,230.00
    "#2AL",,0,168.00,230.00
    "#2CU",,0,216.00,294.00
    Here is my code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    #define MAX_NUMBER_CONDUCTORS	26
    #define COND_DATA_LENGTH		5
    #define COND_FILENAME			"tblConductor.txt"
    #define LOG_FILENAME			"DCA.log"
    
    static FILE *log_file;
    static FILE *cond_file;
    
    int main(void)
    {
    	char cond_name[ MAX_NUMBER_CONDUCTORS ][ COND_DATA_LENGTH ];
    	int ud_flag[ MAX_NUMBER_CONDUCTORS ];
    	float summer_amp[ MAX_NUMBER_CONDUCTORS ];
    	float winter_amp[ MAX_NUMBER_CONDUCTORS ];
    
    	struct tm *newtime;
    	time_t aclock;
    
    	// open logging file
    
    	if ((log_file = fopen( LOG_FILENAME, "a+" )) == NULL)
    		fprintf(stderr, "\nERROR - Cannot open <%s>\n", LOG_FILENAME);
    	else {
    		// write start-up message
    		time( &aclock );
    		newtime = localtime( &aclock );
    		fprintf(log_file, "\nDCA v1.0 BEGINNING %s\n",asctime(newtime));
    		fprintf(stderr, "\nDCA v1.0 BEGINNING %s\n", asctime(newtime));
    		// close logging file
    		fclose (log_file);
    	}
    
    	// open conductor file
    
    	cond_file = fopen( COND_FILENAME, "r" );
    	int i = 0;
    
    	while ( cond_file != NULL ) {
    		fscanf( cond_file, "%s,,%d,%f,%f", cond_name[i], &ud_flag[i], &summer_amp[i], 
    				&winter_amp[i] );
    			if ((log_file = fopen( LOG_FILENAME, "a+" )) == NULL)
    				fprintf(stderr, "\nERROR - Cannot open <%s>\n", LOG_FILENAME);
    			else {
    				// open log file again
    				fprintf(log_file, "Conductor Name: %s  UD Flag: %d  Summer Amp: %f  Winter Amp: %f\n", 
    					cond_name[i], ud_flag[i], summer_amp[i], winter_amp[i]);
    				// close logging file
    				fclose (log_file);
    			}
    		printf( "Conductor Name: %s  UD Flag: %d  Summer Amp: %f  Winter Amp: %f\n", 
    			cond_name[i], ud_flag[i], summer_amp[i], winter_amp[i]); 
    		i++;
    	}
    
    	fclose( cond_file );
    
    	return(0);
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If your input looks like:

    "#2",,0,130.00,170.00

    then you can use scanf to read and parse your lines of code:

    Example:
    Code:
    /* Note: this is a sample of using scanf, this won't directly help your code as is. */
    
    #define BUF_SIZE 256
    void readFile(const char *filename) {
      FILE *file;
      char buffer[BUF_SIZE], *quoted, *ptr;
      float a, b, c, d;
    
      if((file = fopen(filename, "r"))) {
        while(fgets(buffer, BUF_SIZE, file)) {
          if((ptr = strrchr('\"', buffer))) {
            ptr+=2;  // skip '",'
            quoted = buffer+1; // skip '"'
            sscanf(ptr, "%g,%g,%g,%g", &a, &b, &c, &d);
          
          // make use of your info now.
        }
    
        fclose(file);
      }
    }
    The above doesn't guarantee the input is correct but you can figure that part out.
    Last edited by master5001; 09-29-2004 at 02:47 PM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    What I usually do is parse the entire file or portion of the file I need to work with specifically and store it into a char array. Then I iterate through the array looking for specifics entries to remove.

    This is a function I wrote for a script parser I had to write. You probably won'tbe able to copy and paste it, but maybe it'll spark some ideas for you.

    Notes: String is typedefined as a char*, it is not a class. This was written in C, but removes only C++ style comments at current. Using the implementations, it is only an extra step to remove C style /* */ as well.

    Code:
    //////////////////////////////////////////////////////////////////////////
    // Removes comments from string buffer holding the file
    //
    // Parameters
    // strBuffer	->	String to remove comments from.
    //////////////////////////////////////////////////////////////////////////
    
    void RemoveComments(String strBuffer)
    {
    	int iTest, iLoc;
    	
    	for(iTest = iLoc = 0; iTest < iCharCount; ++iTest)
    	{
    		if(strBuffer[iTest] == '/' && strBuffer[iTest+1] == '/')
    		{
    			while(strBuffer[iTest+1] != '\n')
    				++iTest;
    		}
    		else
    		{
    			strBuffer[iLoc] = strBuffer[iTest];
    			++iLoc;
    		}
    	}
    	strBuffer[iLoc] = '\0';
    	iCharCount = iLoc;
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    24
    Thanks for the replies, but I guess that wasn't exactly what I was looking for. Let me try again...

    What specific code could be used to ignore commas and parentheses?

    Also, what code could be used to read in each data as a separate variable?

    I am looking to tweak my existing code and not totally re-write it. I think I'm on the right track, but just need a bit of guidance.

    Thanks again!

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    What you just asked seems to undo what you asked above.

    Do you want the comma as a field separator?
    Do you want the parens - ( ) as field separators?

    You can throw away a field separator, too.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    My code does what you want. I just noticed that I should have a line that looks like this:

    Code:
    *ptr = 0;
    before the line that looks like this:

    Code:
    ptr += 2;
    But other than that little mistake my code will do exactly as you wanted. Obviously it was an example of the theory, but I think thats all you need to get started.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM