473,408 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Need help reading a file.

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

Oct 4 '08 #1
9 1855
Amkcoder wrote:
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).
Oct 4 '08 #2
Amkcoder <fr********************@yahoo.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 4 '08 #3
On Fri, 3 Oct 2008 19:41:03 -0700 (PDT), Amkcoder
<fr********************@yahoo.comwrote:
>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.

--
Remove del for email
Oct 4 '08 #4

"Amkcoder" <fr********************@yahoo.comwrote in message news:
>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?...
How are you printing to the screen?
The standard C function putchar(), printf(), puts and the like will treat a
newline as a line break, however that isn't true of all screen-drawing
functions, nor is it true (usually) if you are accessing the character
buffer directly.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 4 '08 #5
Amkcoder wrote:
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) {
fp = fopen(*argv, "r");
if (fp != NULL) {
while ((rc = fgetc(fp)) != EOF) {
putchar(rc);
}
fclose(fp);
} else {
printf("\nfopen() problem with \"%s\"\n", *argv);
break;
}
}
} else {
fputs("Usage:\n>" ARGV_0, stdout);
puts(" <FILE_0.txt<FILE_1.txt<FILE_2.txt...");
}
return 0;
}

/* END type_2.c */

--
pete
Oct 4 '08 #6
"Amkcoder" wrote:
>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
Oct 4 '08 #7
On Sat, 04 Oct 2008 08:08:28 -0400, pete <pf*****@mindspring.com>
wrote:
>Amkcoder wrote:
>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.
}
}
} else {
fputs("Usage:\n>" ARGV_0, stdout);
puts(" <FILE_0.txt<FILE_1.txt<FILE_2.txt...");
}
return 0;
}

/* END type_2.c */
--
Remove del for email
Oct 4 '08 #8
Amkcoder wrote:
>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 4 '08 #9
Barry Schwarz wrote:
On Sat, 04 Oct 2008 08:08:28 -0400, pete <pf*****@mindspring.com>
wrote:
>Amkcoder wrote:
>>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?
The program was written mostly to be easy to test,
and not so much for using.
>
> 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.
OK.
>
> }
}
} else {
fputs("Usage:\n>" ARGV_0, stdout);
puts(" <FILE_0.txt<FILE_1.txt<FILE_2.txt...");
}
return 0;
}

/* END type_2.c */
--
pete
Oct 5 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Nick Z. | last post by:
I have a file that I want to read a UTF-16 unicode string from. What is the easiest way to accomplish that? Thanks in advance, Nick Z.
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
22
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for...
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
1
by: untitled | last post by:
i wrote an application in c++ that capture a motion from a bmp sequence and outputs a 3DS max secript file contains the motions keys. i have two points here need to be fixed: 1- it is an offline...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
0
by: Sells, Fred | last post by:
I'm running python 2.5 (or 2.4) in an XP environment. I downloaded and installed the .dll's from OpenLDAP-2.4.8+OpenSSL-0.9.8g-Win32.zip and copied the .dll's in c:/windows/system32 as instructed...
1
by: vijayarl | last post by:
Hi Everyone, i have the written this logic : basically a file operation open (CONFIGFILE, "$config_file") or die; while (<CONFIGFILE>) { chomp;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.