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: EvilOrange is an unknown quantity at this point 
Solved Threads: 0
EvilOrange EvilOrange is offline Offline
Newbie Poster

can't get fopen to work

 
0
  #1
Aug 23rd, 2009
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

  1.  
  2. char tmpfilename[10];
  3. char filename[15];
  4.  
  5. if(argv[2] == NULL){
  6. printf("no output file name specified\n");
  7. printf("would you like to specify one now? (y/n)\n");
  8. scanf("%c", &a);
  9. if(a=='y'){
  10. printf("enter the name of the file to be outputted (less than 10 letters)\n");
  11. scanf("%s", &tmpfilename);
  12. sprintf(filename,"%s.ps",tmpfilename);
  13. printf("%s there should be a string here\n",filename);
  14.  
  15. ---> if(!(out = fopen(filename, "w"))){
  16. fprintf(stderr,"Cannot open %s\n",filename);
  17. exit(2);
  18. }
  19. }
  20. if(a=='n'){
  21. printf("ok, exiting\n");
  22. exit(1);
  23. }
  24.  
  25. if(a != 'y' && a != 'n'){
  26. printf("unexpected input...exiting\n");
  27. exit(1);
  28. }
  29. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: can't get fopen to work

 
0
  #2
Aug 23rd, 2009
Post some more code, for example all the other declarations of variables used in this code.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: EvilOrange is an unknown quantity at this point 
Solved Threads: 0
EvilOrange EvilOrange is offline Offline
Newbie Poster

Re: can't get fopen to work

 
0
  #3
Aug 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: can't get fopen to work

 
1
  #4
Aug 23rd, 2009
Originally Posted by EvilOrange View Post
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
On the phone
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: EvilOrange is an unknown quantity at this point 
Solved Threads: 0
EvilOrange EvilOrange is offline Offline
Newbie Poster

Re: can't get fopen to work

 
0
  #5
Aug 23rd, 2009
ok here his the print out i get from terminal:
  1. >./run final.txt
  2. >no output file name specified <--Line 20
  3. >would you like to specify one now? (y/n) <--Line 21
  4. >y <--Line 22
  5. >enter the name of the file to be outputted (less than 10 letters) <--Line 24
  6. >hds <--Line 25
  7. >hds.ps <-there should be a string here <--Line 27 - proving the scanf("%s", &tmpfilename) is working
  8. >Cannot open (null) <--Line 30

and here is the the totality of the file input code:

  1. int main(int argc, char *argv[]){
  2. int i;
  3. char a;
  4. char tmpfilename[SIZE];
  5. char filename[15];
  6.  
  7.  
  8. FILE *fp, *out;
  9. Program prog;
  10.  
  11.  
  12.  
  13. if(!(fp = fopen(argv[1], "r"))){
  14. fprintf(stderr,"Cannot open %s\n",argv[1]);
  15. exit(2);
  16. }
  17.  
  18.  
  19. if(argv[2] == NULL){
  20. printf("no output file name specified\n");
  21. printf("would you like to specify one now? (y/n)\n");
  22. scanf("%c", &a);
  23. if(a=='y'){
  24. printf("enter the name of the file to be outputted (less than 10 letters)\n");
  25. scanf("%s", &tmpfilename);
  26. sprintf(filename,"%s.ps",tmpfilename);
  27. printf("%s there should be a string here\n",filename);
  28.  
  29. if(!(out = fopen(filename, "w"))){
  30. fprintf(stderr,"Cannot open %s\n",filename);
  31. exit(2);
  32. }
  33. }
  34. if(a=='n'){
  35. printf("ok, exiting\n");
  36. exit(1);
  37. }
  38.  
  39. if(a != 'y' && a != 'n'){
  40. printf("unexpected input...exiting\n");
  41. exit(1);
  42. }
  43. }
  44.  
  45. if(!(out = fopen(argv[2], "w"))){
  46. fprintf(stderr,"Cannot open %s\n",argv[2]);
  47. exit(2);
  48. }
Last edited by EvilOrange; Aug 23rd, 2009 at 7:56 pm. Reason: taking out whitespace
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: can't get fopen to work

 
1
  #6
Aug 23rd, 2009
EvilOrange> <--Line 27 - proving the scanf("%s", &tmpfilename) is working
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: EvilOrange is an unknown quantity at this point 
Solved Threads: 0
EvilOrange EvilOrange is offline Offline
Newbie Poster

Re: can't get fopen to work

 
0
  #7
Aug 23rd, 2009
Originally Posted by Aia View Post
EvilOrange> <--Line 27 - proving the scanf("%s", &tmpfilename) is working
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]);
bit embarrased now... had code duplication early on, and deleted the wrong snippet which is shown on 46, and you where correct about the cannot open (null)

it is a sunday and i shouldn't be working after all, thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC