Member Avatar for RenFromPenn

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?

#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;

Recommended Answers

All 11 Replies

fscanf(inFile, "%s", text) Think for a moment, what does text need to be?

> 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

work/project.dsp
work/source/prog.cpp
work/source/document.txt
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.

[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.

Member Avatar for RenFromPenn

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:

#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;
}

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

Member Avatar for RenFromPenn

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?

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

No. I asked you to think:

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.

Member Avatar for RenFromPenn

Okay, well that is how I have it now, so why isn't it displaying on one line?

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().

Member Avatar for RenFromPenn

If I try that instead it results in this error message: fatal error C1903: unable to recover from previous error(s); stopping compilation

post all new code so we can see what you have done.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.