473,404 Members | 2,114 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,404 software developers and data experts.

How to distinguish a null line(just a return) in text file


I have a text file like following and need to read out the names of each
person. Since the only accurate info is there is a null line before
starting a new name, I have written following code to take out the
names, but it doesn't work. Looks like "strcmp(tmp,"\n")==0" is never
reached. How can I do?

Thank you!

my code:

FILE * fp;

char tmp[1024];

if((fp = fopen("Membership.txt", "r")) == NULL)

{

printf("Open Membership.txt error!\n");

_exit(EXIT_FAILURE);

}

if(fscanf(fp,"%s",tmp) == EOF)

{

printf("Date file over or Read data file error.\n");

break;

}

if((strcmp(tmp," \n"))==0){

fscanf(fp,"%s",tmp);

printf("%s \n ",tmp);

}

Membership.txt:

Bell, Dennis A.

NI Gatural Xeatures Inventory

Mason Bldg

PO Box 30456

Okemos, XY12345-7944

636.455.1552

4, 8, 9, 10

Clixia, Wichelle R.

536 W Bbcroft St #3

Toledo, XY 12345-3240

bu********@yahoo.com

Ally, John W.

4 Oxford Rd

East Lansing 48823

P 430.852.7590

al****@um.edu

3, 16, 6, 9, 13

...
--
Posted via http://dbforums.com
Jul 19 '05 #1
3 2973

"gogomei" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

I have a text file like following and need to read out the names of each
person. Since the only accurate info is there is a null line before
starting a new name, I have written following code to take out the
names, but it doesn't work. Looks like "strcmp(tmp,"\n")==0" is never
reached. How can I do?

Thank you!

my code:

FILE * fp;

char tmp[1024];

if((fp = fopen("Membership.txt", "r")) == NULL)

{

printf("Open Membership.txt error!\n");

_exit(EXIT_FAILURE);

}

if(fscanf(fp,"%s",tmp) == EOF)

{

printf("Date file over or Read data file error.\n");

break;

}

if((strcmp(tmp," \n"))==0){

fscanf(fp,"%s",tmp);

printf("%s \n ",tmp);

}


[snip]

Any particular reason for posting this to comp.lang.c++? You're writing C
code not C++ code.

Anyway the correct way to read a line in C is with fgets, fscanf does
something else entirely.

john
Jul 19 '05 #2
John Harrison wrote:

Anyway the correct way to read a line in C is with fgets, fscanf does
something else entirely.


fscanf can certainly be used for reading a line, but not with the "%s"
format specifier. That's only good for opening up gaping security holes
in your program.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
gogomei wrote:
I have a text file like following and need to read out the names of each
person. Since the only accurate info is there is a null line before
starting a new name, I have written following code to take out the
names, but it doesn't work. Looks like "strcmp(tmp,"\n")==0" is never
reached. How can I do?

Thank you! ['C' code snipped]
Membership.txt:

Bell, Dennis A.

NI Gatural Xeatures Inventory

Mason Bldg

PO Box 30456

Okemos, XY12345-7944

636.455.1552

4, 8, 9, 10

Clixia, Wichelle R.

536 W Bbcroft St #3

Toledo, XY 12345-3240

bu********@yahoo.com

Ally, John W.

4 Oxford Rd

East Lansing 48823

P 430.852.7590

al****@um.edu

3, 16, 6, 9, 13

..
--
Posted via http://dbforums.com


In C++, use string, 'ifstream' and 'getline':

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using std::ifstream;
using std::getline;
using std::string;
using std::endl;
using std::cerr;
using std::cout;

int main(void)
{
ifstream data_file("Membership.txt");
if (!data_file)
{
cerr << "Error opening file 'Membership.txt'" << endl;
return EXIT_FAILURE;
}

string text_line;
while (getline(data_file, text_line, '\n')
{
if (text_line.length() == 0)
continue;
Process_Text_Line(text_line);
}
data_file.close();
return EXIT_SUCCESS;
}
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #4

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

Similar topics

5
by: Mark | last post by:
Is it possible to enter a line return into any other field other than text (such as number, currency or date/time)? How can a user be prevented from entering a line return into a field? ...
13
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. ...
3
by: Jow Blow | last post by:
I am trying to make a word wrap type function for a multi line text box field that will be saved to a text file. The word wrap property looks good in the app but when saved to a text file the line...
1
by: Mike L | last post by:
This is for a Win form. I am able to send a string to my multi line text box, but I want the string to have hard returns in it. Here is my code for the string. string s =...
9
by: C.K | last post by:
Can I enter a two-line text description on a button, chr(13) doesn't seem to do anything ?
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
5
by: s99999999s2003 | last post by:
hi i have a file something like this abcdefgh ijklmnopq 12345678 rstuvwxyz ...... ......
8
by: MLH | last post by:
Am trying to import 20,000+ lines of text in a file FTP'd from a UNIX platform to windows via FTP session in a DOS box. About 2000 records have multiple lines in them separated by CRLF's. ...
5
by: Jason Huang | last post by:
Hi, In my C# windows form project, how to use the line return character in a string so the string will go to a new line? Thanks for help. Jason
1
by: joestevens232 | last post by:
I'm stuck on how I can creat a LE object and pass it the line just input..than could i use a for loop and the .push_back to get the lines into the vector? vector<Log_Entry> parse(string); This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.