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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gogomei

    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("Membersh ip.txt", "r")) == NULL)

    {

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

    _exit(EXIT_FAIL URE);

    }

    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

    bugsrus100@yaho o.com



    Ally, John W.

    4 Oxford Rd

    East Lansing 48823

    P 430.852.7590

    allaon@um.edu

    3, 16, 6, 9, 13

    ...


    --
    Posted via http://dbforums.com
  • John Harrison

    #2
    Re: How to distinguish a null line(just a return) in text file


    "gogomei" <member29821@db forums.com> wrote in message
    news:3311227.10 62446176@dbforu ms.com...[color=blue]
    >
    > 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("Membersh ip.txt", "r")) == NULL)
    >
    > {
    >
    > printf("Open Membership.txt error!\n");
    >
    > _exit(EXIT_FAIL URE);
    >
    > }
    >
    > 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);
    >
    > }
    >[/color]

    [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


    Comment

    • Kevin Goodsell

      #3
      Re: How to distinguish a null line(just a return) in text file

      John Harrison wrote:
      [color=blue]
      >
      > Anyway the correct way to read a line in C is with fgets, fscanf does
      > something else entirely.
      >[/color]

      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.

      Comment

      • Thomas Matthews

        #4
        Re: How to distinguish a null line(just a return) in text file

        gogomei wrote:
        [color=blue]
        > 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![/color]
        ['C' code snipped]
        [color=blue]
        > 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
        >
        > bugsrus100@yaho o.com
        >
        >
        >
        > Ally, John W.
        >
        > 4 Oxford Rd
        >
        > East Lansing 48823
        >
        > P 430.852.7590
        >
        > allaon@um.edu
        >
        > 3, 16, 6, 9, 13
        >
        > ..
        >
        >
        > --
        > Posted via http://dbforums.com[/color]

        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("Memb ership.txt");
        if (!data_file)
        {
        cerr << "Error opening file 'Membership.txt '" << endl;
        return EXIT_FAILURE;
        }

        string text_line;
        while (getline(data_f ile, text_line, '\n')
        {
        if (text_line.leng th() == 0)
        continue;
        Process_Text_Li ne(text_line);
        }
        data_file.close ();
        return EXIT_SUCCESS;
        }


        --
        Thomas Matthews

        C++ newsgroup welcome message:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        Working...