Thread: how to detect end of line

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    how to detect end of line

    Hi. I'm reading integers from a file and placing them into an array.
    How do you detect for end-of-line? I've tried checking using '\0' and '\n', neither works.
    If I can detect the end-of-line then I can remove the 0 that adds itself to the array.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <math.h>
    
    int main()
    {
       /*Initialisations*/
       FILE *fin;
       char file_name_in[20]= "test.txt",c;
       long int int_array[10000],temp_array[1000],d,j=0,k=0,temp_integer=0,size_of_array=0;
    
       /* Test to see if file opens */
       fin = fopen(file_name_in, "r");
       if (fin == NULL){
          (void)fprintf(stderr,"Cannot open input file %s\n",file_name_in);
       }
    
       int i=0;
       do{
          c = getc(fin);
          if (((c == ' ')||(c == '\0'))&&(i!=0)){
             temp_integer = 0;
             for (j=0;i>0;j++){
                i-=1;
                temp_integer += (pow(10,i))*temp_array[j];
             }
             int_array[k] = temp_integer;
             printf("\nint_array at position %d is %d\n",k,int_array[k]);
             k++;
          }
          d = atoi(&c);
          temp_array[i]= d;
          i++;
       }while (c!=EOF);
    
       return 0;
    }
    sample input data:
    2 300
    4 800
    10 10
    10 10
    10 10
    10 100
    10 10

    The output:
    int_array at position 0 is 2

    int_array at position 1 is 30004

    int_array at position 2 is 800010

    int_array at position 3 is 10010

    int_array at position 4 is 10010

    int_array at position 5 is 10010

    int_array at position 6 is 100010

    Any Ideas?

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    You can check a string whether it contains an end-of-line sequence by testing for the character '\n', of course, but there isn't a function to tell you that the current file position is "at the end of a line".
    If you have a line-based file format, it's sometimes easiest to use two levels of parsing. First, read in a whole line using the global std::getline function. Then use an std::istringstream to parse that as necessary. Repeat for each line in the input file.
    You should generally never need to ever use eof() directly, of course. Just keep reading lines until the stream evaluates to false in a boolean context.
    Forexample:
    Code:
    std::ifstream input("input.dat");
    std::string line;
    // For each line...
    while (std::getline(input, line)) {    
                                           std::istringstream iss(line);
                                           char c;
        // For each character token...    
                                                     while (iss >> c) {      
      // Do something with c                                      }
                                                      }

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Or use fgets() since this is the C forum.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by a_satari View Post
    Hi. I'm reading integers from a file and placing them into an array.
    Are they integers or are they ascii characters? From your code I get that you are reading a string from a file. I don't get what you are doing in the if clause in the do/while loop, it seems like you would want to advance the file pointer there. But you are probably better of using fgets().

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I don't think if fgets() will work as it just reads the whole file till the end of file marker or just allows to input a single string till the '/n'....

    I didn't check but logically it seems to be like this.....

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well, you can wrap the fgets call inside a while loop condition, then do the conversion inside the loop body. fgets() does what getline does.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mr.777 View Post
    Forexample:
    Code:
    std::ifstream input("input.dat");
    std::string line;
    Not in C you won't.... That's C++ code.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by CommonTater View Post
    Not in C you won't.... That's C++ code.
    Yup... Sorry... My bad ;-)

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    what I'm really trying to ask is, how do you determine end of line?
    In my outputs, the end of line is represented as a 0.
    I want to get rid of this. So far I've used if statements to check for the following :
    '/n'
    '/0'
    NULL
    0x20 - which I think is ascii for end of line.

    none of which have worked.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by a_satari View Post
    what I'm really trying to ask is, how do you determine end of line?
    In my outputs, the end of line is represented as a 0.
    I want to get rid of this. So far I've used if statements to check for the following :
    '/n'
    '/0'
    NULL
    0x20 - which I think is ascii for end of line.

    none of which have worked.
    I don't really understand how 0 marks the end of your lines. So what happens when you read in "10 100". One of those 0s marks the end of your line?

    Anyway, some problems with what you've tried:

    1) '/n', this is a multi-character constant and is invalid. You're looking for '\n', which is the traditional end-of-line marker
    2) Again, '/0' is a multi-character constant and is invalid. You're looking for '\0', which marks the end of a string, but not a line in a text file.
    3) NULL, this is used to indicate an invalid pointer.
    4) 0x20, is the ASCII symbol for a space.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    15
    Thanks itsme86. I don't completely understand how 0 marks the end of line either.
    I think it may be to do with reading the character using getc then converting it to an int using
    atoi.
    I'm going to try a few thing using getw which I'm told reads integers.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think itsme86 was suggesting that 0 doesn't actually mark the end of your lines. The end of a line is marked with '\n'. The end of a string is marked with '\0'. As for getc, it already returns an int (not a char, see: Question 12.1). Trying to use atoi on the result will likely give you a seg fault, since atoi expects a char pointer. getw is most likely not what you want. It is for reading a binary word, not a text word.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. read to end of line problem
    By one1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2003, 04:30 PM
  5. end of line.
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 05:01 AM

Tags for this Thread