473,320 Members | 1,979 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,320 software developers and data experts.

Reading from a text file

Hi,

I am reading strings from a text file using fscanf("%s", currToken);

This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?

Michael

Nov 23 '05 #1
11 2946
In article <11**********************@g47g2000cwa.googlegroups .com>,
Michael McGarry <mi*************@gmail.com> wrote:
I am reading strings from a text file using fscanf("%s", currToken); This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?


The whitespace character (including newline) that terminates the
string will be "put back" into the input stream. Therefor to
determine whether currToken was followed by a carriage return,
read the next character, test to see if it was carriage return,
and if not then ungetc() the character to put it back in the
input stream.

By the way, do you really mean "carriage return", ascii character 13?
Or do you mean "the operating system's end of line sequence, whether that
be carriage return or linefeed or some combination of the two or
something else completely" -- which C compactly encodes as \n ?
--
Programming is what happens while you're busy making other plans.
Nov 23 '05 #2
Michael McGarry wrote:

Hi,

I am reading strings from a text file using fscanf("%s", currToken);

This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?


You can read lines at a time, from a text file,
and find the white space in the resulting string,
if you're really looking to tokenise on white space.

--
pete
Nov 23 '05 #3
Thanks for your reply, I just want to check for the EOL sequence
whatever that may be on some arbitrary OS. I want this code to be
portable.

Thanks for any additional guidance,

Michael

Nov 23 '05 #4
"Michael McGarry" <mi*************@gmail.com> wrote:
I am reading strings from a text file using fscanf("%s", currToken);

This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?


Others have given you an answer to your direct question. But let me make
another suggestion: if you need to know this because you want to read an
entire line, rather than separate words, you should also look at
fgets(). It will read a complete line for you without stopping for other
whitespace than newlines. It will also let (in fact, make) you specify a
buffer size, which means buffer overrun errors are less likely. So does
fscanf(), but it doesn't require it, and as you use it above it does not
know how large your buffer is and will merrily write over the end of it.

Richard
Nov 23 '05 #5
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"Michael McGarry" <mi*************@gmail.com> wrote:
I am reading strings from a text file using fscanf("%s", currToken);

This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?


Others have given you an answer to your direct question. But let me make
another suggestion: if you need to know this because you want to read an
entire line, rather than separate words, you should also look at
fgets(). It will read a complete line for you without stopping for other
whitespace than newlines. It will also let (in fact, make) you specify a
buffer size, which means buffer overrun errors are less likely. So does
fscanf(), but it doesn't require it, and as you use it above it does not
know how large your buffer is and will merrily write over the end of it.


You also need to decide what to do if the input line is longer than
your buffer. In that case, fgets will read as much of the line as it
can, the buffer won't be terminated by a newline character, and the
remainder of the line will be left on the input stream to be read
later.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '05 #6

"Michael McGarry" <mi*************@gmail.com> wrote
I am reading strings from a text file using fscanf("%s", currToken);

This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?

Don't use fscanf().
The library functions are fine as long as you want the narrow purpose for
which they were designed. As soon as you want to do something a bit unusual,
such as check for carriage returns, your best bet is to build a custom
function on top of fgetc().
(You might need to open your file in binary mode, some OSes suppress the
'\r' character).
Nov 23 '05 #7
"Malcolm" <re*******@btinternet.com> writes:
"Michael McGarry" <mi*************@gmail.com> wrote
I am reading strings from a text file using fscanf("%s", currToken);

This returns strings delimited by whitespace. How can I tell if the
string was followed by a carriage return in the file?

Don't use fscanf().
The library functions are fine as long as you want the narrow purpose for
which they were designed. As soon as you want to do something a bit unusual,
such as check for carriage returns, your best bet is to build a custom
function on top of fgetc().
(You might need to open your file in binary mode, some OSes suppress the
'\r' character).


I suspect when the OP asked about a "carriage return", he really meant
an end-of-line, not a literal '\r' character. Opening a text file in
binary mode is seldon a good idea; the possible variations are bigger
than just the Unix vs. Windows "LF", vs. "CR/LF" line terminators.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '05 #8
Hi,

Thanks for all the advice. I just wound up using fgetc() and as soon as
it returned whitespace that was not a space, I assume end of line. In
my case the file contains space seperated fields, so a whitespace that
is not a space must be EOL. Is there explicitly an EOL character that
is OS independent?

I would like to make my design more robust to actually check for an
EOL.

Michael

Nov 23 '05 #9
>Thanks for all the advice. I just wound up using fgetc() and as soon as
it returned whitespace that was not a space, I assume end of line. In
my case the file contains space seperated fields, so a whitespace that
is not a space must be EOL.
Warning: this ignores the existence of tabs, as well as other
characters considered to be white space. This may not be a problem
for *your* files, but you can't assume that for everyone.
Is there explicitly an EOL character that
is OS independent?

I would like to make my design more robust to actually check for an
EOL.


In C, lines from a text file, opened in text mode, as read by
functions like fgetc(), fgets(), fread(), and *scanf() end in '\n'.
It is irrelevant how the line ending is represented *on disk*.
C translates the line ending to '\n' on reading, and from '\n'
to whatever is the appropriate line ending on writing.

All bets are off for files being read in binary mode.

Gordon L. Burditt
Nov 23 '05 #10
Thanks Gordon,

I will explicitly read with fscanf(file,"%s"); until fgetc() returns
'\n'

Michael

Nov 23 '05 #11
Michael McGarry wrote:
Hi,

Thanks for all the advice. I just wound up using fgetc() and as soon as
it returned whitespace that was not a space, I assume end of line. In
my case the file contains space seperated fields, so a whitespace that
is not a space must be EOL. Is there explicitly an EOL character that
is OS independent?

I would like to make my design more robust to actually check for an
EOL.

Michael

Choice of fgetc() and/or fgets() makes a lot of sense to me. Also,
reading from a text file is a very exact exercise. You must know exactly
how this file was constructed in order to read it successfully. There
are few Standards in this regard. You have to know your file.

Generally, text files are comprised of lines of characters, each line
ending with the '\n' character. There may be any number of lines.
Whether the last line in the file is ended with '\n' is an
implementation detail. Usually it is.

Again, we have to know our file to make sense of it. You mention above
separated fields in text files. This suggests to me a database table of
rows and columns presented to us as text. Simply separating fields with
spaces is not normally useful.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 23 '05 #12

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

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.