errno --perror() problem

This is a discussion on errno --perror() problem within the C Programming forums, part of the General Programming Boards category; I want to make a file that is in a map.... I open it this way. Code: FILE *myfile; myfile=fopen(map,"w"); ...

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109

    errno --perror() problem

    I want to make a file that is in a map....
    I open it this way.
    Code:
    FILE *myfile;
    myfile=fopen(map,"w");
    if(myfile==0){
       perror("\n");
       printf("Press a key to continue.\n");
       getch();
       exit(1);
       }
    Now that works great as long as that map exists. I deleted the map and tried to run it and perror gave me an error. Is there a way that i can know wich error has been given. That way i would be able to see wether its a problem with the non-existent map or something else.
    And then i would make the program create the map and then do the fopen thing again.
    I know the error thing has something to do with errno ..its just how do i use errno. (my lib reference wasnt that clear IMO).

    ::edit::
    whoops forgot to place code tags
    BTW : map is data\\whatever_file_user_wants_to_open.whatever_ex tension_i_add
    Last edited by GanglyLamb; 04-01-2003 at 04:37 AM.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    oops alrdy found it
    Code:
    #include <errno.h>
    
    if(errno==ENOENT){
           /*make the dir again*/
           }
    Scuse me for my stupidity .

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    You're telling me that you aren't getting a error message telling that the file was not found? well here I tried and it gave me, "No such file or directory".

    Code:
    #include <stdio.h>
    
    int main(void) {
    	FILE *fp;
    
    	if ((fp = fopen("lelelele.xxx","r")) == NULL) {
    		perror("Fopen: ");
    		return 0;
    	}
    
    	else {
    		printf("We opened the file\n");
    		printf("Aleluia\n");
    		fclose(fp);
    	}
    
    	return 0;
    }

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    yea yeah but it works over here its like this..;
    the app asks for a file taht the user want to make user enters his filanem..... program adds a directory to it so that inthe end the user his file gets into data\hisfile.txt . But i solved it....Only thing is i was compiling and the compiler started acting pretty strange it was like this.
    Code:
    int a=4;
    int lamb=6;
    wasnt the same as
    Code:
    int lamb=6;
    int a=4;
    So i just shut down the comp opened the case and i saw that my cooling fan had stopped spinning.....
    So i laid my hand on cpu and geesh i bet i can fry an egg on it.; so i hope my comp is still intact....

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just for fun, here's an example showing how to collect error messages and report them later rather than immediately.

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    
    #define NUM_FILES 5
    
    int main(void)
    {
      FILE *fp[NUM_FILES];
      char Errors[NUM_FILES][BUFSIZ];
      int i;
        
      for (i = 0; i < NUM_FILES; i++)
      {
        char buf[BUFSIZ];
        sprintf (buf, "File%02d.txt", i);
        
        if ((fp[i] = fopen(buf, "r")) == NULL)
          sprintf (Errors[i], "%s FAILED : (%d) %s", buf, errno, strerror(errno));
        else sprintf (Errors[i], "%s OPEN\n", buf);
      }
      
      for (i = 0; i < NUM_FILES; i++)
      {
        printf ("Status of file %d: %s", i, Errors[i]);
      }
      
      for (i = 0; i < NUM_FILES; i++)
      {
        if (fp[i])
          fclose(fp[i]);
      }
      
      return 0;
    }
    
    /*
     * Output
     Status of file 0: File00.txt FAILED : (2) No such file or directory
     Status of file 1: File01.txt OPEN
     Status of file 2: File02.txt FAILED : (2) No such file or directory
     Status of file 3: File03.txt FAILED : (2) No such file or directory
     Status of file 4: File04.txt FAILED : (2) No such file or directory
     *
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Ive got another question -not related to errno or perror.
    Its about spawn and his families.

    I would like to display the content of a directory.
    First i thought lets keep it siple and use system("dir data");
    But that gives an output that is not very fancy.
    So i was thinking bout spawn (mayb that would do the thing for me). If im alrdy wrong here then excuse me.

    Anyway with spawn
    Code:
    /*main looks like this*/
    int main(int argc, char filename[]);
    
    /*then i make a call to a function in main*/
    int play(filename[]);
    /* in function play i do this*/
    spawnvp(P_WAIT,data,filename[] );
    /*But this gives me an error*/
    So my question is am i on the right track with spawn and all of his families?
    If so what can be wrong with that louzy piece of code?

    ::edit::
    checked the new faq section and there was a thing related to this, but still...
    Last edited by GanglyLamb; 04-01-2003 at 06:41 AM.

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Hmm figured out by now that spawn isnt made for that.
    Instead i came up with this.....

    Code:
    #include <dir.h>
    char buffer[MAXPATH];
    getcwd(buffer, MAXPATH);
    printf("The current directory is: %s\n", buffer);
    char drive[MAXDRIVE];
    char dir[MAXDIR];
    char file[MAXFILE];
    char ext[MAXEXT];
    int flags;
    char directory[]="\\data\\";
    int buffer_length;
    buffer_length=strlen(buffer);
    for(int i=0;i<6;i++){
          buffer[buffer_length+i]=directory[i];
    	}
    printf("%s",buffer);
    flags=fnsplit(buffer, drive, dir, file, ext);
    if(flags && FILENAME && EXTENSION){
    	printf("\n\n%s%s\n",file,ext);
    	}
    This may look like alternative coding but its the only way that i can come up with now to solve my problem.
    So it takes the current directory and put it into buffer then i add a subdirectory data to buffer.... and then i try to split it in pieces with fnsplit. Normally i would see the file that is in <<buffer>>\data\ but output to screen is totally messed up....
    Anyone who sees my mistake?
    Last edited by GanglyLamb; 04-01-2003 at 08:34 AM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you trying to do this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    yup as i alrdy mentioned i looked into the faq. But i didnt really understood everything thats why i started trying to figure it out by myself.... (coz i dont like copy'n-paste).
    Well ill copy'n paste it see what it actually does.... and look what the values are of FA_DIREC and ALL_ATTS....and only if i get whats going on ill put it in my code...

    Guess ill have to take my blender to get a mixture of both things.

    Thx anyway

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i didnt really understood everything thats why i started trying to figure it out by myself.... (coz i dont like copy'n-paste).
    The idea behind the FAQ here is that it gives you enough information to take a snippet of code and make something with it. If you've read the FAQ, and can't understand the code/concept, let me know, and I'll try to add more of an explanation to it. There's no point in having the FAQ if doesn't answer your questions.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Well actually that faq made me thing bout the whole thing and i solved it without using any of the faq code. So to read files out of a directory that is constructed like this:

    drive:\dir\subdir\my_app.exe
    \data\data.txt

    Where drive, dir and subdir are not the same on every system.
    +there mayb several dirs and subdirs.

    Code:
    do{
    	printf("Choose your quiz.\n");
    	struct ffblk ffblk;
    	int found;
    	char directory1[]="DATA\\";
    	char directory[]="\\DATA\\*.txt";
    	char buffer[MAXPATH];
    	char *quiz_file;
    	char *extension;
    	char name[BUFSIZ];
    	getcwd(buffer, MAXPATH);
    	strcat(buffer,directory);
    	found=findfirst(buffer,&ffblk,0);
    	while (!found)
    	{
    		if((quiz_file=strchr(ffblk.ff_name,'.'))!=NULL){
    			*quiz_file=' ';
    			*++quiz_file=' ';*++quiz_file='\0';
    			strcpy(name,ffblk.ff_name);
    			textcolor(14);/*RED*/
    			cprintf("%s",name);
    			}
    			found = findnext(&ffblk);
    		}
    So in a way the faq was really good .. else i wouldnt know about functions like findfirst and findnext.

    Keep up the good work (bout the faq), those questions really make me think bout something like how would i do that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to clean errno?
    By kotoko in forum C Programming
    Replies: 20
    Last Post: 05-14-2009, 12:21 PM
  2. errno function, error TLS/non TLS
    By fran.b in forum C Programming
    Replies: 13
    Last Post: 03-25-2009, 08:06 AM
  3. /usr/bin/ld: errno: TLS definition in /lib/libc.so.6
    By nasim751 in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 03:15 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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