Thread: clueless file access problem

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    clueless file access problem

    so for some reason my program likes to tell me "Could not open file.". it shouldn't be doing this, at least from what i can tell...here's my code:
    Code:
    void SearchFiles(HWND hwnd, char *keywords)
    {
         FILE *fileptr, *xFileptr;
         char file[255] = {0}, data[255] = {0};
         
         if((fileptr = fopen("dirlist.sby", "r")) == NULL)
         {
             MessageBox(NULL, "There was an error retrieving the directory listing.\n",
               "Error!", MB_OK | MB_ICONEXCLAMATION);
         }
         else
         {
             while(fgets(file, 255, fileptr) != '\0')
             {
                 if((xFileptr = fopen(file, "r")) == NULL)
                 {
                     MessageBox(NULL, "Could not open file.\n", "Error!", MB_OK | MB_ICONEXCLAMATION);
                 }
                 else
                 {
                     while(fgets(data, 255, xFileptr) != '\0')
                     {
                         if(strstr(data, keywords) != NULL)
                         {
                             SendMessage(GetDlgItem(hwnd, ID_LISTOUT), LB_ADDSTRING, 0, (LPARAM)file);
                         }
                     }
                 }
                 fclose(xFileptr);
             }
             fclose(fileptr);
         }
    }
    ?? what am i doing wrong ?? i tried the debug function of Dev-C++ but it just freezes. everytime i run this, after telling me it can't open the file windows asks me to send them a report of my program crashing. =\


    hah...so the problem was the method in which i was reading from the file. the fgets() function for some reason was messing it up. after adding/using this function:
    Code:
    int fixgets(char input_array[], int length, FILE *file_name)
    {
       register int position=0;
    
       if (fgets(input_array,length,file_name) != NULL)
         {
         while(input_array[position] != 0)
    	  {              if (input_array[position] < 32) input_array[position]=0;
    	  else ++position;
    	  }
         }
       /* The length of the string is returned. */
       return position;
    }
    it worked :]
    Last edited by willc0de4food; 04-17-2006 at 08:21 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm thinking you have a list of filenames, one filename per line. If you do, and you're using fgets() to read filenames one by one, then fgets() is likely storing a newline ('\n') to the end of your input, causing your fopen() to fail. (You're trying to open "myfile.txt\n") You sort of correct this with your fixgets function because you wipe the newline.

    Also, in checking the return type of fgets(), I personally say != NULL as opposed to != '\0'.

    See fgets().
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Just check if the last character in each filename is '\n', if so change it to '\0' and all C functions will treat it as if though it never existed.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    doing != NULL rather than != 0 or != '\0' gives the warning of comparison between a pointer and integer. the others do not.

    thx tho
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char text[10];
    	
    	if(fgets(text, 5, stdin) == NULL) return 1;
    	
    	return 0;
    }
    Code:
    $ gcc -Wall -o fgets.exe fgets.c
    $
    No errors. fgets() returns a pointer to a char. Thus '\0' (A character, though my gcc will not warn me as long as it's '\0'. If it's something like 'a', I get invalid comparision between a pointer and a integer. (fgets() and 'a', respectively))

    Interesting you should get that error, as you use NULL to check the return of strstr(), which is the same type as fgets(). There is also a FAQ Article on this.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > doing != NULL rather than != 0 or != '\0' gives the warning of comparison between a pointer and integer
    Try including all the correct header files then.
    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.

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by Salem
    > doing != NULL rather than != 0 or != '\0' gives the warning of comparison between a pointer and integer
    Try including all the correct header files then.
    and those would be?


    adding -Wall to the compiler made the warnings go away


    although i found that using fgets to control the loop it was only reading the first line of each file as there was white space between the first line and the next line for some files. this was stopping the loop.
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM