Thread: File being filled with NULLs

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    File being filled with NULLs

    Gudday
    Slowly getting my program together. I have various bits that work seprately but having trouble getting them in a coherent whole.
    The code below looks for files, in a directory, of form *.RPT. If found any file names are placed in a file called RPT.dat. Then file RPT.dat should be read one line at a time so the each RPT file can be analysed (haven't added this bit yet but it works separately). The program creates the RPT.dat file successful.
    The code also checks that RPT.dat exists, if so it continues, if not it leaves.
    If RPT.dat exists the program should grab the first line (in fact the only line in my run) and print out a line to say that it has found it.
    Instead the program adds 4074(?) NULLS to the RPT.dat file and prints out 4074(?) messages.
    Code:
    int main (int argc, char *argv[])
    {
      FILE *rptFile; /* store any  RPT file names found in here*/
      FILE *inputFile, *errorFile, *indexFile;
      char *CONFIG_FILE = "RPT.dat";
      char fileline[255]; //line in RPT file
      char rptline[25];   //name of RPT file
      struct _finddata_t fileData = {0};
      char rpt_file[8] = "RPT.dat";
    
      // start a search for all files with an extension of "rpt"
      intptr_t searchHandle = _findfirst("*.rpt", &fileData);
      // if the search started
      if(searchHandle != -1)
      {
           // print a banner
    	//puts("Found the following files:");
    	do
    	{
                    // print the file name
    		//puts(fileData.name);
    		rptFile = fopen (rpt_file, "w+");  /* open data file "rptFile.dat" */
    		fprintf(rptFile, "%s\n", fileData.name);  /* write RPT file name to rptFile.dat file  */
    
    	}
            // look for any more files
    	while(_findnext(searchHandle, &fileData) == 0);
            // stop the search after all have been enumerated
    	_findclose(searchHandle);
      }
      else
      {
           // if we couldn't start a search, print out the reason
    	perror("Couldn't enumerate files: ");
      }
      //printf("looking for RPT.dat existence");
       if( (access(CONFIG_FILE, F_OK)) != -1 )
       {
    	 printf("An RPT.dat file exists\n");
    	 while(fgets(rptline, 25, rptFile)!=NULL) //loop for reading a line up to end of file
          {
    		printf("RPT file exists, getting name of it out of file\n");
    	  }
    
    
       }
       else
       {
    	 printf("An RPT.dat file does not exist");
    	 return 0;
       }
    
    fclose(rptFile);
    return 0;
    }
    Obviously I have done something absolutely silly but cannot see it.

    Please advise.
    The RPT.dat is attached (called RPT.txt to get loaded) as is a screen shot of the output.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    man fopen(3)
    ``w+'' Open for reading and writing. The file is created if it does not
    exist, otherwise it is truncated. The stream is positioned at
    the beginning of the file.
    1. You keep deleting what's there
    2. You don't close it, so eventually the fopens will fail with no more file handles.


    char *CONFIG_FILE = "RPT.dat";
    char rpt_file[8] = "RPT.dat";
    There's no benefit to having the same thing twice.

    Keep it simple.
    Open it for writing (just the once), do some writing, close it.
    THEN
    Open it for reading, do some reading, close it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Salem
    Thank you for your reply. That has worked. I put in an extra fclose and fopen and it seems ok.
    Code:
    do
    	{
                    // print the file name
    		//puts(fileData.name);
    		rptFile = fopen (rpt_file, "w+");  /* open data file "rptFile.dat" */		fprintf(rptFile, "%s\n", fileData.name);  /* write RPT file name to rptFile.dat file  */
    	}
            // look for any more files
    	while(_findnext(searchHandle, &fileData) == 0);
            // stop the search after all have been enumerated
    	_findclose(searchHandle);
    	fclose(rptFile);
    and
    Code:
    if( (access(CONFIG_FILE, F_OK)) != -1 )
       {
    	 printf("An RPT.dat file exists\n");
    	 rptFile = fopen (rpt_file, "r+");
    	 while(fgets(rptline, 25, rptFile)!=NULL) //loop for reading a line up to end of file
          {
    		printf("RPT file exists, getting name of it out of file\n");
    	  }
    
    
       }
       else
       {
    	 printf("An RPT.dat file does not exist");
    	 return 0;
       }
    So much to learn still.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 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. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10: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