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

G

gogomei

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

(e-mail address removed)



Ally, John W.

4 Oxford Rd

East Lansing 48823

P 430.852.7590

(e-mail address removed)

3, 16, 6, 9, 13

...
 
J

John Harrison

gogomei said:
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
 
K

Kevin Goodsell

John said:
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
 
T

Thomas Matthews

gogomei said:
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

(e-mail address removed)



Ally, John W.

4 Oxford Rd

East Lansing 48823

P 430.852.7590

(e-mail address removed)

3, 16, 6, 9, 13

..

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
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top