grimx Posted September 30, 2007 Report Share Posted September 30, 2007 (edited) I'm using Anjuta1.2.4 #include <stdio.h> #define fSize 80 void Clear_Input(void); void ReadFile(void); void WriteFile(void); void N_Crypt(void); void D_Crypt(void); int main(void) { char option; int running = 1; while(running) { puts("===========Menu============"); puts("1) ReadFile 2) WriteFile"); puts("3) N_Crypt 4) D_Crypt"); puts("5) Quit"); puts("==========================="); option = getchar(); if((option < '1') || (option > '5')) { Clear_Input(); puts("Invalid Input"); continue; } Clear_Input(); switch(option) { case '1': ReadFile(); break; case '2': puts("WriteFile"); break; case '3': puts("N_Crypt"); break; case '4': puts("D_Crypt"); break; case '5': puts("GoodBye"); running = 0; break; default: puts("Invalid Input"); } } puts("Done"); return 0; } void Clear_Input(void) { while(getchar() != '\n') continue; } void ReadFile(void) { FILE *fp; char fName[fSize]; int letter = 0; printf("Enter File To Read: "); fgets(fName, fSize, stdin); printf("Opening %s ....\n", fName); if((fp = fopen(fName, "r")) == NULL) { printf("Unable To Open ( %s )\n", fName); return; } while((letter = getc(fp)) != EOF) putchar(letter); fclose(fp); printf("\nDone Reading Contents of ( %s )\n", fName); } void WriteFile(void) { } void N_Crypt(void) { } void D_Crypt(void) { } my problem is in the ReadFile(). When i get a file name from the keyboard (say "Hello.txt") it does not open the file, but when i hardcode the filename "Hello.txt", then it reads the file. whats going on. Edited September 30, 2007 by grimx Quote Link to comment Share on other sites More sharing options...
grimx Posted September 30, 2007 Author Report Share Posted September 30, 2007 yes the Hello.txt is in the same dir as my program and i do have read-write access to the file. Quote Link to comment Share on other sites More sharing options...
neddie Posted September 30, 2007 Report Share Posted September 30, 2007 The fgets function reads characters from the stream stream up to and including a newline character So when you try to open the file, it looks for the filename "Hello.txt\n" which of course it can't find. Yes, it's ugly :) Quote Link to comment Share on other sites More sharing options...
grimx Posted October 1, 2007 Author Report Share Posted October 1, 2007 doah!!!!!!!! I forgot about that . thanx Quote Link to comment Share on other sites More sharing options...
grimx Posted October 1, 2007 Author Report Share Posted October 1, 2007 (edited) How do i fix it : i tryed int size = strlen(fName) fName = 'backslash n''; Edited October 1, 2007 by grimx Quote Link to comment Share on other sites More sharing options...
grimx Posted October 1, 2007 Author Report Share Posted October 1, 2007 (edited) I fixed it with int size = 0; size = strlen(fName); fName[size - 1] = 'backslash 0'; problem solved.. Thanx again. Edited October 1, 2007 by grimx Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted October 1, 2007 Report Share Posted October 1, 2007 (edited) Don't mean to intrude, since the problem is solved, but rather than this: fName[size - 1] = 'backslash 0'; I would do this: if(fName[size - 1] == '\n') fName[size - 1] = 'backslash 0'; so your code will be more portable. Edited October 1, 2007 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
neddie Posted October 1, 2007 Report Share Posted October 1, 2007 I would do this: if(fName[size - 1] == '\n') fName[size - 1] = 'backslash 0'; so your code will be more portable. Wow, didn't think of portability! :) But I don't quite get that - what do you do if that character isn't \n, do nothing? Could you explain why? Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted October 2, 2007 Report Share Posted October 2, 2007 Right. Do nothing. You can actually make it an if - else if you wanted, but let's say you put this in a sub/function and called it from other places. This would ensure that you aren't stripping characters you don't want to strip. In this particular case, it is perfectly safe to be used the way it is, because you know they have to hit Enter to get it to accept the input, so the '\n' will always be there, but if you wanted to make a function to strip '\n' for other purposes, you would need to make sure that's what you're stripping. Quote Link to comment Share on other sites More sharing options...
neddie Posted October 2, 2007 Report Share Posted October 2, 2007 Ah ok, I get it now. I thought when you said "portable" that you meant portable across systems, like win or mac, and thought it was something to do with the \n and the \r\n. Doh. But I get it now, that's a good idea. Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted October 3, 2007 Report Share Posted October 3, 2007 Well, I did probably use a bad term. :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.