943,789 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 849
  • C RSS
Aug 23rd, 2009
0

can't get fopen to work

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
EvilOrange is offline Offline
30 posts
since Jan 2009
Aug 23rd, 2009
0

Re: can't get fopen to work

Post some more code, for example all the other declarations of variables used in this code.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 23rd, 2009
0

Re: can't get fopen to work

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
Reputation Points: 10
Solved Threads: 0
Light Poster
EvilOrange is offline Offline
30 posts
since Jan 2009
Aug 23rd, 2009
1

Re: can't get fopen to work

Click to Expand / Collapse  Quote originally posted by EvilOrange ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 23rd, 2009
0

Re: can't get fopen to work

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
Reputation Points: 10
Solved Threads: 0
Light Poster
EvilOrange is offline Offline
30 posts
since Jan 2009
Aug 23rd, 2009
1

Re: can't get fopen to work

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 23rd, 2009
0

Re: can't get fopen to work

Click to Expand / Collapse  Quote originally posted by Aia ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
EvilOrange is offline Offline
30 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Why need temperory variables when enterring values for the matrix?
Next Thread in C Forum Timeline: how to fork a grandchild process





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC