Question:
Read in a file and output its contents on to the screen in C programming?
anonymous
1970-01-01 00:00:00 UTC
Read in a file and output its contents on to the screen in C programming?
Four answers:
Lisa A
2012-11-23 18:53:28 UTC
This is what you get for copying some random code from someone, and not even reading it.



It is not possible that you know how to write to a file, but not stdout. That's how I know you dodn't write this.



This doesn't output to the screen because there is no code that ourtputs to the screen. There is only code that writes to a file.
MichaelInScarborough
2012-11-24 12:33:10 UTC
You could do the following:

Replace this

while(!feof(file_in))

{fscanf(file_in, "%s", text);

}



with this:

char buffer[100];

while(!feof(file_in))

{fscanf(file_in, "%s", buffer);

strcat(text, buffer);

}



or in case the file size is less or equal than 10000



fgets(text, sizeof(text), file_in);



Have fun!
jplatt39
2012-11-23 20:30:55 UTC
If you want to read it in character by characterthe line should read:

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



Two points. I renamed string text to char c of course, but with NO & (address of operator) the function will not change the value in whatever you send it. So send it the address of what you are trying to write to.
Aaron
2012-11-23 12:32:39 UTC
First and foremost, you MUST absolutely check your file to make sure that it opened. so, if((file_in = fopen(name, "r")) == NULL) { printf("File could not be opened"); exit(1) }

because if you don't check that and you try to open a file that does not exist it will give you segmentation faults, and obviously you have no idea what is, but it is thought to be an unrecoverable error.



*EDIT* ALSO, NEVER EVER use magic numbers (char name[50] and char text[10000]) What a waste of space... what if you only read 3 characters, thats 9997 bits of memory that could've been used for something more useful. What you should do, although you don't even need those arrays for this, is use define. (i.e. #define MAXNAME 50 or #define MAXTEXT 100) When you use define, there is no semicolon at the end of the line.

Now, for your future coding if you want to allocate a text array of 10000 characters, don't do that. Allocate the memory as you go. In order to do so, you need to learn malloc and realloc.

I will give you the gist of malloc and realloc.

malloc is the initial allocation of memory, so generally what I do is allocate, initially, 80 bits. So I would have my #define MAXLEN 80, then if ((int x = (int *) malloc(MAXLEN * sizeof(int))) == NULL) {

printf("Allocation failed.\n"); exit(1);

} Because if your system is unable to allocate 80 bits of memory, it is another one of those unrecoverable errors. But that is the initial allocation. SO if that passes you now have your 80 bits, allocated. Next I would run a check to make sure I wasn't at the end of the file, and then if I reached the end of my allocated memory and I haven't reach EOF I would realloc 80 more bits of memory.



next, you can use 2 methods, either strtok, which I would not suggest for you. Rather, use getc.



Keep in mind getc returns the ascii code of whatever letter it reads, in other words, its return value is an integer.



So, I will not give you the code for the getc function, you should research it a little bit, get to know the function. However, I will give you this... run the getc through a while loop saying while(getc != EOF) *that is not the actual code, but its pretty close to it.* Its pretty simple, shouldn't be tough for you to get.



then, to print it is simple just a printf statement that prints each character on its own. I forget whether or not you need %d or %c, but should be simple. Keep in mind, getc will also get the '\n' character so you don't need to worry about that.


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