Thread: Getting filenames from a file

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    7

    Getting filenames from a file

    Ok, what I want to do is pretty simple but I'm doing something wrong because it won't work. I have a small text file that looks like so:

    032032002
    FILENAME01.EXT
    FILENAME02.EXT

    basically the top line has 3 integer values in it, and the last one is the number of files to read. Then after that line there are filenames listed in the order in which they should be opened. My code currently uses fopen along with fgets to get the appropriate strings. When I run a test program just to see if the correct strings are being read this is what i get for output:

    int1: 32
    int2: 32
    int3: 2
    file0: FILENAME01.EXT
    file1: FILENAME02.EXT

    So i know I'm reading the correct number of bytes, but in the code, when I try and make another call to fopen using one of the filenames from the file, it will give me the error that the file doesn't exist, whereas I know for a fact that it does as well as that if I just type the name of the file in directly it works just fine.

    Here's some sample code:

    Code:
    void testIO(char * filename)
    {
         FILE * tempfile = 0;
         FILE * tempfile2 = 0;
         intone = 0;
         inttwo = 0;
         intthree = 0;
         char temptxt[100];
         char * temptxt2 = 0;
    		
         if ((tempfile = fopen(filename, "r")) != NULL)
         {
    		
              // read in the first integer
              if( fgets( temptxt, 4, tempfile ) != NULL)
              {
                   intone = atoi(temptxt); 
                   printf( "int1: " );
                   printf( "%i", intone);
                   printf( "\n" );
              }
              // read in the second integer
              if( fgets( temptxt, 4, tempfile ) != NULL)
              {
                   inttwo = atoi(temptxt); 	
                   printf( "int2: " );
                   printf( "%i", inttwo); 
                   printf( "\n" );
              }
    		
              // read in the third integer
              if( fgets( temptxt, 5, tempfile ) != NULL)
              {
                   printf( "int3: " );
                   intthree = atoi(temptxt); 	
                   printf( "%i", intthree);
                   printf( "\n" );
              }
              for(int i=0; i < (intthree); i++)
              {
                   //read the line
                   temptxt2 = new char[100];
                   if( fgets( temptxt2, 100, tempfile ) != NULL)
                   {
                        //put in the null character at the end
                        for(int x=0; x<100; x++)
                        {
                             if(temptxt2[x] == '\n')
                                  temptxt2[x] = '\0';
                        }
                        printf( "file" );
                        printf( "%i", i);
                        printf( ": ");
                        printf( "%s", temptxt2);
                        printf( "\n" );
    
                        //OPEN FILE HERE THIS IS WHERE I GET THE ERROR
                        if((tempfile2 = fopen(temptxt2, "r")) == NULL)
                             printf( "ERROR FILE DOESN't EXIST" );
                        else
                             //DO SOMETHING WITH THE DATA IN IT
                   }
                   delete temptxt2;
                   temptxt2=0;
              }
              fclose(tempfile);
         }
    }
    Yeah, I keep getting an error there and I really don't know why. At first I thought it was because there was a whole bunch of unused space at the end of the string(temptxt2) so I changed all the filenames to be the same length and updated the program so it always put the null character in the same place(because the same place in all the strings was the last character in the string). Anyway though, help will be greatly appreciated, and thank you very much in advance.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by vinsta18
    So i know I'm reading the correct number of bytes, but in the code, when I try and make another call to fopen using one of the filenames from the file, it will give me the error that the file doesn't exist, whereas I know for a fact that it does as well as that if I just type the name of the file in directly it works just fine.

    Here's some sample code:

    Code:
    
                        //OPEN FILE HERE THIS IS WHERE I GET THE ERROR
                        if((tempfile2 = fopen(temptxt2, "r")) == NULL)
                             printf( "ERROR FILE DOESN't EXIST" );
                        else
                             //DO SOMETHING WITH THE DATA IN IT
                   }
    Yeah, I keep getting an error there and I really don't know why. At first I thought it was because there was a whole bunch of unused space at the end of the string(temptxt2) so I changed all the filenames to be the same length and updated the program so it always put the null character in the same place(because the same place in all the strings was the last character in the string). Anyway though, help will be greatly appreciated, and thank you very much in advance.

    You might make your messages a little more informative:

    Code:
                        if((tempfile2 = fopen(temptxt2, "r")) == NULL) {
                             printf( "can't open <%s>\n", temptxt2);
    
                        }
                        else {
                          printf("Opened file <%s>\n", temptxt2);
                             //DO SOMETHING WITH THE DATA IN IT
                        }
    In some cases, depending on the file and the compiler and the operating system, there may be a carriage return at the end of the name in the input buffer. Then you might get something like this on the output:

    int1: 32
    int2: 32
    int3: 2
    file0: <>
    can't open <>
    >ile1: <FILENAME01.EXT
    >an't open <FILENAME01.EXT
    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. 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
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  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