1

I debugged a function and it is working. So yay, teaching myself C seems to be going along well. But I want to make it better. That is, it reads a file like this:

want 
to 
program
better

And puts each individual line of string into an array of strings. Things get weird when I print things out however. As far as I read, strcpy() should just copy a string until the \0 character. If that is true, why is the following printing the string want and \n? It is like strcpy() also copied \n and it is hanging in there. I want to get rid of that.

My code for copying the file is below. I didn't include the entire program because I don't believe that is relevant for what is happening. I know the problem is in here.

void readFile(char *array[5049]) 
{
    char line[256]; //This is to to grab each string in the file and put it in a line. 
    int z = 0; //Indice for the array

    FILE *file;
    file = fopen("words.txt","r");

    //Check to make sure file can open 
    if(file == NULL)
    {
        printf("Error: File does not open.");
        exit(1);
    }
    //Otherwise, read file into array  
    else
    {
        while(!feof(file))//The file will loop until end of file
        {
           if((fgets(line,256,file))!= NULL)//If the line isn't empty
           {
             array[z] = malloc(strlen(line) + 1);
             strcpy(array[z],line);
             z++;
           }    
        }
    }
    fclose(file);
}

So now, when I do the following:

     int randomNum = rand() % 5049 + 1;

     char *ranWord = words[randomNum];
     int size = strlen(ranWord) - 1; 
     printf("%s",ranWord);
     printf("%d\n",size);
     int i; 
     for(i = 0; i < size; i++)
     {
          printf("%c\n", ranWord[i]);
     }

It prints out:

these 
6
t
h
e
s
e

Shouldn't it be printing out the following instead?

 these6
 t
 h
 e
 s
 e

So the only thing I can figure is that when I put the strings into an array, it put the \n in there too. How can I get rid of that?

As always, with respect. GeekyOmega

GeekyOmega
  • 1,235
  • 6
  • 16
  • 34
  • 3
    Is `\n` == `\0`, if no, then copy. Simple. If you want to avoid that, write your own `strcpy()`. Or manipulate the '\0' character position if newlines are always at the end. – jn1kk Jul 31 '12 at 15:42
  • Don't use feof(); you don't need it, if you don't need to make a difference between error and EOF. putting fgets() is the loop is good enough, and much simpler. – wildplasser Jul 31 '12 at 16:48
  • I consider this closed. Thanks to all answering it. – GeekyOmega Aug 07 '12 at 16:03

2 Answers2

8

fgets reads the \n in also, it is part of your input file. If you want to get rid of it, do something like:

int len = strlen(line);
if (len > 0 && line[len-1] == '\n') line[len-1] = '\0';
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • even better: `if (len > 0 && line[len-1] == '\n') line[--len] = '\0';` – wildplasser Jul 31 '12 at 15:50
  • Thanks to you both. That indeed fixed the problem. Is fgets() a really bad way to do this? Could I have avoided something like this is I used fread() instead? – GeekyOmega Jul 31 '12 at 15:56
  • `fread` has the same behavior. You can avoid this problem by using a language other than C which has more advanced string/io processing built-in (python, Java, ...). – Keith Randall Jul 31 '12 at 16:22
  • 1
    I don't know about other people's opinions, but fread() sounds like an even worse way to do it; having to read the bytes one at a time then convert is a lot more tedious and error prone than reading in a line and simply removing the trailing newline. You have to deal with the newlines *somehow*, so it may as well be in as clean of a way as possible. – Dennis Meng Jul 31 '12 at 16:25
  • Thanks everyone. As I said, I am learning. Someone told me that fread() was better, but I personally prefer fgets(). I appreciate everyone's help. – GeekyOmega Jul 31 '12 at 18:16
1

When you read the first line, for instance, what you're actually reading is "want\n", because the line break is part of the line. So you get "want\n\0". The same is valid for the other lines (except for the last one, unless your file have a blank line in the end).

Rodrigo Guedes
  • 1,169
  • 2
  • 13
  • 27