file input/output

This is a discussion on file input/output within the C Programming forums, part of the General Programming Boards category; I am suppose to write a program that can save a list of names and its relevant info(into a structure). ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    file input/output

    I am suppose to write a program that can save a list of names and its relevant info(into a structure). However, i have problem retrieving the "name" datas out when i open the program again.Here's a summary of what i wrote.PLEASE TAKE NOTE OF LINE A AND LINE B.

    Code:
    /* structure declaration */
    typedef struct {
    				char marker;
    				char id;
    				char name[21];
    			
    			}data;
    /* function protype */
    void quit(data seat[12]);
    
    main(void)
    {
    
    
    /* declare variables */
    	data seat[12];
    	int seatId, i,choice, num_Seat=0;
    
    	/* file pointers for marker,id and name respectively */
    	FILE *fpM, *fpID, *fpName;
    	
    		
    	/* load data from data file */
    	fpM = fopen("markerData", "rt");
    	fpID = fopen("idData","rt");
    	fpName = fopen("nameData","rt");
    
    
    	if ((fpM == NULL) || (fpID == NULL) || (fpName == NULL))
    	{printf("Error in loading data.\n");
    		
    	return 0;}
    
    
    	for (i=0;i<12;i++)
    	{
    			seat[i].id = fgetc(fpID);
    		
    			seat[i].marker = fgetc(fpM); 
    
    			/* LINE A */
    			fscanf(fpName,"%s",seat[i].name);
    					
    		
    			
    			
    	  }
    
    
    	fclose(fpM);
    	fclose(fpID);
    	fclose(fpName);
    	
    
    .............
    /*
     the main() receives inputs from user and managed to store them successfully in the array structure seat[12]. Then, the program quits and saves the data by calling quit().
    */
    
    
    }
    
    
    void quit(data seat[12])
    {
    	
    	int i;
    	FILE *fpM, *fpID, *fpName;
    
    
    		
    	fpM = fopen("markerData", "wt");
    	fpID = fopen("idData","wt");
    	fpName = fopen("nameData","wt");
    	printf("Quiting....\n");
    	if ((fpM == NULL) || (fpID == NULL) || (fpName == NULL))
    	{printf("Error in saving data.\n");
    	return ;}
    	
    	
    	/* saving data */
    	for (i=0;i<12;i++)
    	{
    	fprintf(fpM,"%c",seat[i].marker);
    	
    	fprintf(fpID,"%c",i+1);
    	
                     /* LINE B */
    	fprintf(fpName,"%s",seat[i].name);
    
    		}
    
    	fclose(fpM);
    	fclose(fpID);
    	fclose(fpName);
    
    }

    My program cannot retrieve the datas from seat[i].name properly.
    Strange characters are stored inside seat[i].name instead of what i intend.

    Code:
     
    
    
    However, if i changed the 
    line a:
    fscanf(fpName,"%s",seat[i].name);
     
    to 
    
    fscanf(fpName,"%s\n",seat[i].name);
    
    
    AND
    
    line b:
    fprintf(fpName,"%s",seat[i].name);
    
    to 
    
    fprintf(fpName,"%s\n",seat[i].name);
    
    
    My program will be able to retrieve the datas out properly.


    Can someone tell me why is it necessary to add the newline character? As i write each string into the file *fpName using fprintf(), is the NULL character added? Somehow i think my fscanf() doesnt recognise any NULL character during the loading at line a. That's why i couldnt retrieve the names out.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Can someone tell me why is it necessary to add the newline character?

    To separate the strings with whitespace.

    >As i write each string into the file *fpName using fprintf(), is the NULL character added?

    No.

    By the way, NULL is a macro which expands to an implementation-defined null pointer constant. A null character is byte with all bits set to 0, used to terminate a character string.

    >Somehow i think my fscanf() doesnt recognise any NULL character during the loading at line a. That's why i couldnt retrieve the names out.

    It sounds to me like strings may have been concatenated together. For example, "Bob", "Bill", and "Jim" were written as "BobBillJim".

    I don't think the \n is necessary in the call to fscanf because strings are whitespace-delimited.
    http://www.eskimo.com/~scs/C-faq/q12.17.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    If i manage to write my strings of name with a NULL character at each of the names(so that they r not contenated), will fscanf() recognise each NULL character and seperate the strings into different array(i.e seat[i].name) or the only way is to separate the strings with a whitespace character?

    If it is possible, how do i write the strings with the NULL characters added to the file?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >NULL character

    Please reread what I wrote about the terminology.

    >If it is possible, how do i write the strings with the NULL characters added to the file?

    I added a null character to the end of the string this way.
    Code:
    #include <stdio.h>
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "w");
       if ( file )
       {
          const char *text[] = { "The", "quick", "brown", "dog"};
          size_t i;
          for ( i = 0; i < sizeof text / sizeof *text; ++i )
          {
             fprintf(file, "%s", text[i]);
             fputc('\0', file);
          }
          fclose(file);
       }
       return 0;
    }
    >If i manage to write my strings of name with a NULL character at each of the names(so that they r not contenated), will fscanf() recognise each NULL character and seperate the strings into different array(i.e seat[i].name) or the only way is to separate the strings with a whitespace character?

    In a simple test I tried, I think not.
    Code:
    #include <stdio.h>
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char buffer[BUFSIZ];
          while ( fscanf(file, "%s", buffer) == 1 )
          {
             printf("buffer = \"%s\"\n", buffer);
          }
          fclose(file);
       }
       return 0;
    }
    But that may be because the %s directive "[m]atches a sequence of non-white-space characters", and I don't believe a null character is considered white-space.
    white-space characters (space, horizontal tab, new-line, vertical tab, and form-feed)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 06:28 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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