Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to distinguish a null line(just a return) in text file

Reply
Thread Tools

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

 
 
gogomei
Guest
Posts: n/a
 
      09-01-2003

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

http://www.velocityreviews.com/forums/(E-Mail Removed)



Ally, John W.

4 Oxford Rd

East Lansing 48823

P 430.852.7590

(E-Mail Removed)

3, 16, 6, 9, 13

...


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-01-2003

"gogomei" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-01-2003
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.

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      09-02-2003
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
>
> (E-Mail Removed)
>
>
>
> Ally, John W.
>
> 4 Oxford Rd
>
> East Lansing 48823
>
> P 430.852.7590
>
> (E-Mail Removed)
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
It seems that XPath does not distinguish between an inexistent pathand a null string? Ramon F Herrera XML 13 06-28-2012 01:14 PM
Does C distinguish between a C source file and a text file? amit C Programming 4 08-11-2006 07:27 PM
distinguish between binary text and regular text zvika Perl Misc 2 12-12-2004 04:20 PM
Distinguish text URLs from non-text URLs? Kaidi Java 5 01-04-2004 10:15 AM



Advertisments