| | |
Unable to open file with fopen
![]() |
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Hi,
Okay, so what I am actually trying to do is open a file, convert any lowercase text to uppercase, and then print it on screen. I thought it best to first just begin by trying to open a file and display the contents on screen. I figured that once I had accomplished that, I would try to then tackle the uppercase problem.
Well, things aren't working out so well. I have the program, but it keeps displaying the portion that reads, "The file was not opened successfully. Please check that you entered the file name correctly."
I have a text document in the same folder as the cpp file and all of the rest of it, so I can't see what the problem is. I should probably mention that I am not using quotes when I enter the file name. I am just typing document.txt and then pressing enter. Can someone please have a look and tell me where I have gone wrong?
Okay, so what I am actually trying to do is open a file, convert any lowercase text to uppercase, and then print it on screen. I thought it best to first just begin by trying to open a file and display the contents on screen. I figured that once I had accomplished that, I would try to then tackle the uppercase problem.
Well, things aren't working out so well. I have the program, but it keeps displaying the portion that reads, "The file was not opened successfully. Please check that you entered the file name correctly."
I have a text document in the same folder as the cpp file and all of the rest of it, so I can't see what the problem is. I should probably mention that I am not using quotes when I enter the file name. I am just typing document.txt and then pressing enter. Can someone please have a look and tell me where I have gone wrong?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { FILE *inFile; char fileName[13]; char text; printf("\nEnter a file name: "); gets(fileName); inFile = fopen(fileName, "r"); /*This opens the file */ if (inFile == NULL) /*Checks that the file exists*/ { printf("\nThe file %s was not opened successfully.", fileName); printf("\nPlease check that you entered the file name correctly.\n"); exit(1); } while (fscanf(inFile, "%s", text) !=EOF) /*Reads and displays the file*/ printf("%s\n", text); fclose(inFile); return 0;
> I have a text document in the same folder as the cpp file and all of the rest of it
Where is the executable, in relation to the .cpp file?
Where is the main project file, in relation to the .cpp file?
If you've got something like
Then you might try these filenames instead
../source/document.txt
./source/document.txt
But before you do, make your filename array larger.
Where is the executable, in relation to the .cpp file?
Where is the main project file, in relation to the .cpp file?
If you've got something like
C Syntax (Toggle Plain Text)
work/project.dsp work/source/prog.cpp work/source/document.txt work/debug/prog.exe
../source/document.txt
./source/document.txt
But before you do, make your filename array larger.
[edit]^^^ beat me to it
[/edit]
>>fscanf(inFile, "%s", text)
>>printf("%s\n", text);
I don't know why the file doesn't open, but the above line is guarenteed to crash. Why? Because "%s" tells scanf() and printf() the next argument (text) is a null-terminated character array, and all you passed is a single character. You can fix that problem by declaring text something like this:
As for the problem you reported, try entering the full path to the file. But you have to increase the size of FileName so that it can hold all the characters.
Finally, never ever use gets() because its so unsafe. Use fgets() instead like this:
[/edit]>>fscanf(inFile, "%s", text)
>>printf("%s\n", text);
I don't know why the file doesn't open, but the above line is guarenteed to crash. Why? Because "%s" tells scanf() and printf() the next argument (text) is a null-terminated character array, and all you passed is a single character. You can fix that problem by declaring text something like this:
char text[255]; As for the problem you reported, try entering the full path to the file. But you have to increase the size of FileName so that it can hold all the characters.
Finally, never ever use gets() because its so unsafe. Use fgets() instead like this:
fgets(filename, sizeof(filename), stdin); Now the drawback with fgets() is that it will append the '\n' (Enter key), so you have to strip it off before using the filename variable. Last edited by Ancient Dragon; Jan 4th, 2009 at 3:32 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Okay, here is what I've got now. I'm not sure which folder the exe is in, so I went with some really overkill here and put a copy of the document.txt file in every folder. Now it is doing something completely strange. It opens the file, sort off. Instead of displaying the text properly, it displays it like this:
T
h
i
s
i
s
a
t
e
s
t
.
Along with the file name itself, it also has some very odd characters displayed on every line. It looks like they might be [| over and over again.
Here is my updated code:
T
h
i
s
i
s
a
t
e
s
t
.
Along with the file name itself, it also has some very odd characters displayed on every line. It looks like they might be [| over and over again.
Here is my updated code:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { FILE *inFile; char fileName[25]; char text[255]; printf("\nEnter a file name: "); gets(fileName); inFile = fopen(fileName, "r"); /*This opens the file */ if (inFile == NULL) /*Checks that the file exists*/ { printf("\nThe file %c was not opened successfully.", fileName); printf("\nPlease check that you entered the file name correctly.\n"); exit(1); } while (fscanf(inFile, "%c", text) !=EOF) /*Reads and displays the file*/ printf("%c\n", text); fclose(inFile); return 0; }
Last edited by RenFromPenn; Jan 4th, 2009 at 3:51 pm. Reason: Mistyped some stuff
>I had %s before, but you implied that I should change it so I went with %c.
No. I asked you to think:
If you tell fscanf() to format a string (that's what "%s" means) obtaining it from inFile, what would the variable text need to be. I assumed you could think, well I need a variable that can hold a string.
And then you would go to char text and change it to char text[x], where x is the capacity of it.
No. I asked you to think:
C Syntax (Toggle Plain Text)
fscanf(inFile, "%s", text) Think for a moment, what does text need to be?
And then you would go to char text and change it to char text[x], where x is the capacity of it.
![]() |
Similar Threads
- The Fastest way to read a .txt File (C++)
- turning userinput into a text file (C)
- Bubble sort & File output jibrish errors??? (C)
- Adding to linked list from external file (C)
- Image read and operation on it (C)
- memory allocation (C)
- Urgent! Change using class (C++)
Other Threads in the C Forum
- Previous Thread: Need examples of menu choices contected to linked lists
- Next Thread: can't copy existing file into new file
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






