473,748 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3007
In article <11************ **********@g47g 2000cwa.googleg roups.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_Keit h) 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*******@btin ternet.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_Keit h) 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

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

Similar topics

6
12749
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
7056
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 file... Thank you to all of you who can help me with this one...
19
10367
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
0
1752
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 Process=Interim Level=10 Technique=Fletching Knowledge=Woodworking Device=Sawhorse Primary components=Refined Maple
1
6760
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 listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
50
5011
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
2
2493
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() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
4
3302
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 errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
4
12808
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 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
3
2836
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 (!file.eof ()) { do {
0
8994
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.