Need help reading a file.

A

Amkcoder

I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I want to do this so the line will break on the console
while reading it in a character at a time.
I am using fgetc on a file opened for reading in text mode.
What could be the problem?
Can anyone help?...
 
S

Sjouke Burry

Amkcoder said:
I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I want to do this so the line will break on the console
while reading it in a character at a time.
I am using fgetc on a file opened for reading in text mode.
What could be the problem?
Can anyone help?...
If you want to see each character, read in binary mode, in text
mode the cr/lf chars are translated(on MS dos/win).
 
K

Keith Thompson

Amkcoder said:
I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I want to do this so the line will break on the console
while reading it in a character at a time.
I am using fgetc on a file opened for reading in text mode.
What could be the problem?
Can anyone help?...

Show us your code.
 
B

Barry Schwarz

I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I want to do this so the line will break on the console
while reading it in a character at a time.
I am using fgetc on a file opened for reading in text mode.
What could be the problem?
Can anyone help?...

Unless you have a syntax error on line 42 (in which case I will
immediately patent my crystal ball), you will get more helpful answers
by posting a small compilable program that exhibits the behavior in
question.
 
O

osmium

Amkcoder said:
I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I want to do this so the line will break on the console
while reading it in a character at a time.
I am using fgetc on a file opened for reading in text mode.
What could be the problem?
Can anyone help?...

Maybe this link will help. Look especially at the section on
Representations. You can ignore the stuff on Unicode. Also, search google
groups for the hundreds of responses others who had the same problem have
received over the years. In short, depending on your OS, there may be magic
going on under the hood that you don't know about.

http://en.wikipedia.org/wiki/New_line_character
 
B

Barry Schwarz

Amkcoder said:
I am trying to read a file a character at a time,
I want to read in the new line character.
I am printing out each character to the screen.
For some reason it will not print out the new line.
I want to do this so the line will break on the console
while reading it in a character at a time.
I am using fgetc on a file opened for reading in text mode.
What could be the problem?
Can anyone help?...

/* BEGIN type_2.c */

#include <stdio.h>

#define ARGV_0 "type_2"

int main(int argc, char *argv[])
{
int rc;
FILE *fp;

if (argc > 1) {
while (*++argv != NULL) {

You apparently intend to process all the files the user specifies when
he invokes the program.
fp = fopen(*argv, "r");
if (fp != NULL) {

Wouldn't it be a good idea to tell the user which file the following
data came from?
while ((rc = fgetc(fp)) != EOF) {
putchar(rc);
}
fclose(fp);
} else {
printf("\nfopen() problem with \"%s\"\n", *argv);
break;

Why break? Just because one file could not be opened is no reason to
skip processing any remaining files.
 
C

CBFalconer

Amkcoder said:
I am trying to read a file a character at a time, I want to read
in the new line character. I am printing out each character to
the screen. For some reason it will not print out the new line.
I want to do this so the line will break on the console while
reading it in a character at a time. I am using fgetc on a file
opened for reading in text mode. What could be the problem?

Copying a text file is extremely simple, especially if you can
attach a file to stdin with a command such as:

$ copytext <filename.ext

A suitable program is:

#include <stdio.h>

int main(void) {
int ch;

while (EOF != (ch = getchar())) putchar(ch);
return 0;
}

Note you need not pay any attention to new lines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top