Thread: How do you strip newline from char array?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    How do you strip newline from char array?

    I am reading in a txt file into an array - programData[100][6] from Main. I cannot change the element sizes per specs. Is the newline char even being saved since ALL words being read in are 5 characters long. Thus fgets() sees the newline from the txt file in the 6th position and appends a '\0' to the 6th position (over the newline) because the array size is only 6. So maybe I do not need to append my own '\0' char to the end since it would seem fgets() is doing that for me. So how to I get fptr to advance to the next line???? It seems that fgets is pausing on the first line on the second call and then advancing on the third call, this might be why counter when printed out shows 127 and then I get a seg fault since counter is integrated into fgets() and I've exceeed the array bounds. Any help appreciated. Thanks!!!


    Code:
    void readFileIntoArray(char programData[][6])
    {
         
         FILE *fptr;
         int counter = 0;
    
    
         
         fptr = fopen("a:\\269data.txt","r");
    
         if (fptr == '\0')
         {
              printf("*****269data.txt file error - aborting program!*****\n");
              system("pause");
              exit(0);
         }
         
         if(feof(fptr))
         {
              printf("*****No data to read!*****\n");
              system("pause");
         }
     
         printf("\n\nTable 1 Initial Program Data as Read from 269data.txt...\n\n");
       
         while(!feof(fptr))     
         {
              fgets(programData[counter],6,fptr);
              programData[counter][6-1] = '\0'; //This doesn't work and I get a seg fault
              counter++; //counter seems to pass 100 and stops at 127 before crash                    
         }
             
    }
    Last edited by pityocamptes; 04-20-2006 at 10:34 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > if (fptr == '\0')
    Use == NULL

    > while(!feof(fptr))
    See the FAQ

    > fgets(programData[counter],6,fptr);
    The problem here is that if fgets() can't read the \n, then it's left for the next call.
    So you're reading 5 characters on one call, then the \n on the next call - hence the alternating pattern you see.

    Code:
    char buff[BUFSIZ];
    while ( counter < 100 && fgets( buff, sizeof buff, fptr ) != NULL ) {
      if ( strlen(buff) == 6 ) {
        /* 5 chars, and a newline*/
        strncpy( programData[counter], buff, 5 );
        programData[counter][5] = '\0';
        counter++;
      } else {
        /* report incorrect line */
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Thanks! As far as NULL vs '\0' our teacher said to use '\0' as NULL is not recognized by all compilers/platforms. Any truth to that? Thanks again.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by pityocamptes
    Thanks! As far as NULL vs '\0' our teacher said to use '\0' as NULL is not recognized by all compilers/platforms. Any truth to that? Thanks again.
    I'm pretty sure NULL is standard C... or something. It should be defined but what is defined as is implementation specific.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    Ok. I got it to work with fread() where COL is 6 and I changed "counter" to "index" (trying to make it more readable).



    Code:
         while(fread(programData[index],6,1,fptr) != NULL)      
         {          
              programData[index][COL-1] = '\0';                  
              index++;                           
         }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > NULL is not recognized by all compilers/platforms. Any truth to that?
    Maybe if your teacher has a fondness for some ancient pre-ANSI compiler.
    It's part of ANSI-C, so there should be no excuse.

    Besides, '\0' sends the wrong stylistic message as '\0' represents the nul character, not the NULL pointer. Using '\0' makes it seem like fgets() is returning a char!.

    I suppose you could just use a bare 0 to represent a NULL pointer, which every C compiler should accept, and which is actually the definition of a NULL pointer in C++.

    > fread(programData[index],6,1,fptr) != NULL
    See, this is wrong as well - fread() returns the number of items read, not a pointer!
    You were better off using fgets() to read a text file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    NULL is defined *somewhere* in the lib files... just think of it as doing

    Code:
    #define NULL (void*)0
    Or something along those lines.


    IMHO better programming practice would be to use

    Code:
    ptr = (type_name*)0;
    instead of

    Code:
    ptr = 0;
    ptr = NULL;
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > IMHO better programming practice would be to use
    IMO, it would not.
    http://c-faq.com/null/nullptrmacro.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. scanf , char, newline, space
    By Harry Reve in forum C Programming
    Replies: 1
    Last Post: 09-11-2011, 06:49 AM
  3. Remove newline char at the end
    By Ducky in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2009, 02:29 PM
  4. make a char* which will hold newline
    By swagatob in forum C Programming
    Replies: 8
    Last Post: 10-10-2004, 11:24 AM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM