| | |
can't get fopen to work
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
every thing works till the fopen, the sprintf works as i'm printing to check that. i just doing know what is causing this not to work
it prints "cannot open (null)" but the filename var does have something assigned to it.
any help will be welcomed
it prints "cannot open (null)" but the filename var does have something assigned to it.
any help will be welcomed
c Syntax (Toggle Plain Text)
char tmpfilename[10]; char filename[15]; if(argv[2] == NULL){ printf("no output file name specified\n"); printf("would you like to specify one now? (y/n)\n"); scanf("%c", &a); if(a=='y'){ printf("enter the name of the file to be outputted (less than 10 letters)\n"); scanf("%s", &tmpfilename); sprintf(filename,"%s.ps",tmpfilename); printf("%s there should be a string here\n",filename); ---> if(!(out = fopen(filename, "w"))){ fprintf(stderr,"Cannot open %s\n",filename); exit(2); } } if(a=='n'){ printf("ok, exiting\n"); exit(1); } if(a != 'y' && a != 'n'){ printf("unexpected input...exiting\n"); exit(1); } }
•
•
•
•
there is only 2 var missing and thats:
char a;
FILE *out;
argv[2] is NULL which is why this bit of code comes into play
Nick: Doctor, can you cure my cough? It is a horrible cough, I cannot sleep at night.
Dr. Hell: Sorry, Nick, I cannot prescribe the proper medication without seeing you first, make an appointment for tomorrow.
Nick: OK.
Next day at Dr Hell's consult.
Dr Hell: Who are you?
Nick's buddy: I am Nick's best friend. He told me that you needed to see him first, in order for you to prescribe medicine for his cough. So he sent me with his best photograph taken last year at the summer's party.
EvilOrange> there is only 2 var missing and thats:
That's not enough.
Dr Hell guesses that it has to do with...
scanf("%s", &tmpfilename); Last edited by Aia; Aug 23rd, 2009 at 7:42 pm.
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
ok here his the print out i get from terminal:
and here is the the totality of the file input code:
C Syntax (Toggle Plain Text)
>./run final.txt >no output file name specified <--Line 20 >would you like to specify one now? (y/n) <--Line 21 >y <--Line 22 >enter the name of the file to be outputted (less than 10 letters) <--Line 24 >hds <--Line 25 >hds.ps <-there should be a string here <--Line 27 - proving the scanf("%s", &tmpfilename) is working >Cannot open (null) <--Line 30
and here is the the totality of the file input code:
c Syntax (Toggle Plain Text)
int main(int argc, char *argv[]){ int i; char a; char tmpfilename[SIZE]; char filename[15]; FILE *fp, *out; Program prog; if(!(fp = fopen(argv[1], "r"))){ fprintf(stderr,"Cannot open %s\n",argv[1]); exit(2); } if(argv[2] == NULL){ printf("no output file name specified\n"); printf("would you like to specify one now? (y/n)\n"); scanf("%c", &a); if(a=='y'){ printf("enter the name of the file to be outputted (less than 10 letters)\n"); scanf("%s", &tmpfilename); sprintf(filename,"%s.ps",tmpfilename); printf("%s there should be a string here\n",filename); if(!(out = fopen(filename, "w"))){ fprintf(stderr,"Cannot open %s\n",filename); exit(2); } } if(a=='n'){ printf("ok, exiting\n"); exit(1); } if(a != 'y' && a != 'n'){ printf("unexpected input...exiting\n"); exit(1); } } if(!(out = fopen(argv[2], "w"))){ fprintf(stderr,"Cannot open %s\n",argv[2]); exit(2); }
Last edited by EvilOrange; Aug 23rd, 2009 at 7:56 pm. Reason: taking out whitespace
EvilOrange> <--Line 27 - proving the scanf("%s", &tmpfilename) is working
Dealing with the format
EvilOrange> >Cannot open (null) <--Line 30
Are you sure is line 30 and not line 46?
Dealing with the format
%s in scanf() you must pass the argument as plain tmpfilename or as &tmpfilename[0] , but not as &tmpfilename EvilOrange> >Cannot open (null) <--Line 30
Are you sure is line 30 and not line 46?
fprintf(stderr,"Cannot open %s\n",argv[2]); Last edited by Aia; Aug 23rd, 2009 at 8:10 pm.
•
•
Join Date: Jan 2009
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
EvilOrange> <--Line 27 - proving the scanf("%s", &tmpfilename) is working
Dealing with the format%sin scanf() you must pass the argument as plaintmpfilenameor as&tmpfilename[0], but not as&tmpfilename
EvilOrange> >Cannot open (null) <--Line 30
Are you sure is line 30 and not line 46?fprintf(stderr,"Cannot open %s\n",argv[2]);
it is a sunday and i shouldn't be working after all, thanks.
![]() |
Similar Threads
- Need 2 techies for tech-support, work from home (Tech / IT Consultant Job Offers)
- fopen problem (PHP)
- Why fopen() doesnt work? (C++)
- Help on PHP fopen (PHP)
- printing to the screen after using fopen() (PHP)
- using fopen() function (C)
- fopen: No such file or directory (*nix Software)
Other Threads in the C Forum
- Previous Thread: Why need temperory variables when enterring values for the matrix?
- Next Thread: how to fork a grandchild process
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming stack standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






