943,852 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2933
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 4th, 2009
0

Unable to open file with fopen

Expand Post »
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?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main()
  6. {
  7. FILE *inFile;
  8. char fileName[13];
  9. char text;
  10.  
  11. printf("\nEnter a file name: ");
  12. gets(fileName);
  13. inFile = fopen(fileName, "r"); /*This opens the file */
  14. if (inFile == NULL) /*Checks that the file exists*/
  15.  
  16. {
  17. printf("\nThe file %s was not opened successfully.", fileName);
  18. printf("\nPlease check that you entered the file name correctly.\n");
  19. exit(1);
  20. }
  21. while (fscanf(inFile, "%s", text) !=EOF) /*Reads and displays the file*/
  22.  
  23. printf("%s\n", text);
  24. fclose(inFile);
  25. return 0;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Unable to open file with fopen

fscanf(inFile, "%s", text) Think for a moment, what does text need to be?
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 4th, 2009
0

Re: Unable to open file with fopen

> 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
  1. work/project.dsp
  2. work/source/prog.cpp
  3. work/source/document.txt
  4. work/debug/prog.exe
Then you might try these filenames instead
../source/document.txt
./source/document.txt

But before you do, make your filename array larger.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 4th, 2009
0

Re: Unable to open file with fopen

[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: 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jan 4th, 2009
0

Re: Unable to open file with fopen

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:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main()
  6. {
  7. FILE *inFile;
  8. char fileName[25];
  9. char text[255];
  10.  
  11. printf("\nEnter a file name: ");
  12. gets(fileName);
  13. inFile = fopen(fileName, "r"); /*This opens the file */
  14. if (inFile == NULL) /*Checks that the file exists*/
  15.  
  16. {
  17. printf("\nThe file %c was not opened successfully.", fileName);
  18. printf("\nPlease check that you entered the file name correctly.\n");
  19. exit(1);
  20. }
  21. while (fscanf(inFile, "%c", text) !=EOF) /*Reads and displays the file*/
  22.  
  23. printf("%c\n", text);
  24. fclose(inFile);
  25. return 0;
  26. }
Last edited by RenFromPenn; Jan 4th, 2009 at 3:51 pm. Reason: Mistyped some stuff
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Unable to open file with fopen

Never use gets(). Forget that exist. It is not safe at all, not even in "throw away code".

fscanf(inFile, "%c", text) change red to s
Last edited by Aia; Jan 4th, 2009 at 4:10 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 4th, 2009
0

Re: Unable to open file with fopen

I had %s before, but you implied that I should change it so I went with %c. Anyway, I put it back now and that made some progress. The text is displaying almost right. Here is what I have now:
This
is
a
test.

Al least it looks almost normal :-) Any ideas why it isn't displaying all on one line?
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Unable to open file with fopen

>I had %s before, but you implied that I should change it so I went with %c.

No. I asked you to think:
  1. fscanf(inFile, "%s", text) Think for a moment, what does text need to be?
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 4th, 2009
0

Re: Unable to open file with fopen

Okay, well that is how I have it now, so why isn't it displaying on one line?
Reputation Points: 10
Solved Threads: 0
Light Poster
RenFromPenn is offline Offline
40 posts
since Dec 2008
Jan 4th, 2009
0

Re: Unable to open file with fopen

Okay, well that is how I have it now, so why isn't it displaying on one line?
Because %s in scanf() will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).

It's best if you use fgets() instead of scanf().
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

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: Need examples of menu choices contected to linked lists
Next Thread in C Forum Timeline: can't copy existing file into new file





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


Follow us on Twitter


© 2011 DaniWeb® LLC