Question:
Why wont my C program read in file and print what file says?
felmkern
2012-11-26 05:02:46 UTC
My program is below. I need to read in a file (chosen by the user) and then my program should print the file's contents to screen. Please be aware I have only had 4 powerpoint slides teaching on this so my code might be completely wrong!

When I run this code it tells me that the file has been read but it only prints one character and the character is never one from the file itself but just a random character such as # * & á


/* A program which encrypts a text file named by the user */
#include
#include
#include

#define maxname 50 //sets maximum number of file name characters to 50
#define maxtext 10000//sets maximum number of characters read in to 10000

int main(void)

{
char name[maxname];//char name[max 50]
char text[maxtext];//char name[max 10000]
FILE *file_in;
FILE *file_out;
int c;

printf("Please enter the name of the file you wish to encrypt \nincluding the file type e.g. .txt : ");//ask user for the name of the file
scanf("%s", name);//name entered given to array name


if((file_in = fopen(name, "r")) == NULL)//file open assigned to file_in and null check performed. Null check stops the program running for an infinite number of times.
{
printf("\nError. Could not open file.\n");
exit(EXIT_FAILURE);
}
else {
do
{
fscanf(file_in, "%c", text);
for(c=0; c!=EOF; c++);
{
printf("%c", text[c]);
}
}
while(!feof(file_in));
}

fclose(file_in);
return 0;
}
Eight answers:
anonymous
2012-12-03 16:41:19 UTC
for(c=0; c!=EOF; c++);



check this statement
Robc
2012-11-30 15:39:00 UTC
One issue is that you are mixing C comments and C++ comments.

/* this is a C comment */

// this is a C++ comment which will give a syntax error in C.

Both of the above work in C++.

Are you compiling this as a C program or as a C++ program? It appears that you are compiling as C++.



To the code. You do not need the for loop since you are only reading one character at a time..

do

{

fscanf(file_in, "%c", text); /* will read one character into text[0] */

printf("%c", text[0]);

}while(!feof(file_in));
?
2016-04-30 11:28:09 UTC
Understanding the foundational reading skills will allow your youngster to concentrate on understanding the topic they're reading rather than battle with understanding what finally resulting in a more worthwhile and satisfying reading experience and this is in what this program Children Learning Reading from here https://tr.im/MOdQb is based.

Children Learning Reading use techniques to greatly help your son or daughter read and improve his examining, knowledge, and punctuation power in the first school years.

Although the Children Understanding Reading program is situated about your child understanding the small appears that make up each word there are a few phrases in the British language that just can't be learnt in this manner (rhythm for example). To be able to aid one to teach your son or daughter these extraordinary words an manual is presented to the most frequent words that must be discovered by sight.

That advantage may be especially useful in the event that you and your son or daughter are experiencing a specific word. You realize that if the phrase is contained in the guide then it's something that can't be learnt applying the Children Learning Reading method.
roger
2012-11-26 08:21:07 UTC
one problem is here:



fscanf(file_in, "%c", text);

%c is for a single character

text is a pointer to an array of characters -- oops.

this reads one character from the file and puts it in text[0]



for(c=0; c!=EOF; c++);

{

printf("%c", text[c]);

}

since EOF is usually defined as -1 this will loop through all the integers till you overflow an int and c becomes -1

and you will write past the end of text[]
Galen
2012-11-26 05:53:01 UTC
Some tips:



1) If you just want to read the file one record at a time, you might do better with fgets() instead of fscanf. It is simple to call and would execute faster too.



2) After you read a record, you could iteratively process each character.



3) Do you want to write the output to the user terminal, or to a file on disk? Your call to printf() will do the first. If instead you want to write to a disk file, you need at the beginning to use fopen() to open/create the output file, then (inside your loop) to use something like fputs() to write each record.



If you really have to read and write one character at a time, you should probably use getchar() together with either putchar() (to write to the user terminal) or putc() (to write to a disk file)



4) You may have intended to use the variable "file_out" for an output file, but after declaring it, your program never uses it again. If you are writing to the user terminal, you don't need this variable. Otherwise you need to open/create an output file. You would call fopen() and assign its return value to this variable



This looks like a programming exercise so rather than providing all the details on these stdio functions, or just rewriting your program, I'll leave the rest to you.
anonymous
2014-09-18 07:01:59 UTC
Hi,

There are numerous documented benefits and advantages of teaching children to read early on, and teaching them to reading using phonics and phonemic awareness instructions. It is clear that early language and reading ability development passes great benefits to the child as they progress through school at all grades, and that early language and reading problems can lead to learning problems later on in school.



For a simple, step-by-step program that can help your child learn to read visit this web site: http://readingprogram.toptips.org

Bye
srikanth
2012-11-26 05:33:40 UTC
Your code is all screwed up. Do you even know how to write C coding?

When you typed in: printf("Please enter the name of the file you wish to encrypt \nincluding the file type e.g. .txt



Your syntax is all wrong and you use backslashes instead of slashes.

Learn to program before you type in such dumb questions.

If you knew programming, you would be laughing hysterically at your question.
?
2016-08-02 16:36:15 UTC
If you wish to read it in character by characterthe line should learn: fscanf(file_in,"%c",&c); Two facets. I renamed string text to char c of direction, but and not using a & (tackle of operator) the perform won't alternate the worth in whatever you send it. So ship it the address of what you are trying to write to.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...