How do you strip \n from char array?

This is a discussion on How do you strip \n from char array? within the C Programming forums, part of the General Programming Boards category; I am reading in a txt file into an array - programData[100][6] from Main. I cannot change the element sizes ...

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

    How do you strip \n 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 11:34 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-03-2005, 12:15 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21