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

Discussion in 'C++' started by gogomei, Sep 1, 2003.

  1. gogomei

    gogomei Guest

    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





    Ally, John W.

    4 Oxford Rd

    East Lansing 48823

    P 430.852.7590



    3, 16, 6, 9, 13

    ...


    --
    Posted via http://dbforums.com
     
    gogomei, Sep 1, 2003
    #1
    1. Advertising

  2. "gogomei" <> wrote in message
    news:...
    >
    > 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
     
    John Harrison, Sep 1, 2003
    #2
    1. Advertising

  3. 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.
     
    Kevin Goodsell, Sep 1, 2003
    #3
  4. 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
    >
    >
    >
    >
    >
    > Ally, John W.
    >
    > 4 Oxford Rd
    >
    > East Lansing 48823
    >
    > P 430.852.7590
    >
    >
    >
    > 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
     
    Thomas Matthews, Sep 2, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaidi
    Replies:
    5
    Views:
    475
    Andrew Thompson
    Jan 4, 2004
  2. Carl
    Replies:
    21
    Views:
    997
    Patricia Shanahan
    Aug 24, 2006
  3. amit
    Replies:
    4
    Views:
    352
    Keith Thompson
    Aug 11, 2006
  4. zvika
    Replies:
    2
    Views:
    135
    J├╝rgen Exner
    Dec 12, 2004
  5. Ramon F Herrera
    Replies:
    13
    Views:
    1,811
    Manuel Collado
    Jun 28, 2012
Loading...

Share This Page