1

The array I'm working with is:

char arr[91][12];

So I populate my array from a file using a for loop like so:

for(i = 0; fgets(arr[i], 12, inFile); i++){}

When I want print anything from that array, it's automatically escaping to the next line.

to test that array:

for(i = 0; fgets(arr[i], 12, inFile); i++){
    printf("%s", arr[i]);
}

//one
//two
//three etc.

//want it to be 'one two three etc.'

I'm trying to use strpbrk() to find \n for each string in the array and change it to \0 like this:

for(i = 0; fgets(arr[i], 12, inFile); i++){
if(strpbrk(arr[i], "\n") != NULL ){
    arr[i][strpbrk(arr[i], "\n")] = '\0';
    }
printf("%s", arr[i]);
}

but this gives me errors. Is there a better way to do what I'm trying to do?

nzondlo
  • 4,166
  • 4
  • 18
  • 20

3 Answers3

5

It's actually much simpler than you seem to think. If the newline is at the end (as it is when reading with fgets) just change the last character to the string terminator:

arr[i][strlen(arr[i]) - 1] = '\0';

The -1 is because arrays (like strings can be considered to be) starts their indexing on zero and goes to length - 1.


To make sure you really have a newline, you can use e.g. strpbrk like you do (but of course not in the way you do it as explained in other answers). But I think it would be a better idea to use strrchr. First because it's simpler (only searching for character not string), second because it starts searching from the end:

char *newlineptr = strrchr(arr[i], '\n');
if (newlineptr != NULL)
    *newlineptr = '\0';

Or the fastest way of all:

size_t length = strlen(arr[i]);
if (arr[i][length - 1] == '\n')
    arr[i][length - 1] = '\0';
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    +1. I think `fgets` would have been better if it returned the number of characters read rather than `NULL` or the buffer you gave it. – dreamlax Feb 20 '13 at 02:20
  • @dreamlax: undoubtedly. The POSIX 2008 [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) provides the length as the return value, or -1 (intriguingly, it is documented as -1, not EOF) on error or EOF. – Jonathan Leffler Feb 20 '13 at 04:19
4

strpbrk() returns a pointer to the newline, so you should use it just once:

char *nl;

if ((nl = strpbrk(arr[i], "\n")) != NULL)
    *nl = '\0';

Alternatively, the newline will be the last character in the string, so this works too:

size_t len = strlen(arr[i]);
if (arr[i][len-1] == '\n')
   arr[i][--len] = '\0';

Note that this checks whether you got a newline; fgets() stops reading when it has run out of space for anything except the NUL '\0', even if it has not read a newline yet. This also gives you the length of the string (so does strpbrk(), indirectly; it is nl - arr[i] - 1). Note that strpbrk() and its relative strcspn() are more complex than strlen().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

strpbrk returns a pointer to the '\n'. So change:

arr[i][strpbrk(arr[i], "\n")] = '\0';

to:

*strpbrk(arr[i], "\n") = '\0';
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • `strpbrk` can return `NULL` though, and this will happen if the line was too large to fit into the buffer provided to `fgets()`, so best to check for `NULL` first. – dreamlax Feb 20 '13 at 02:18
  • @dreamlax: The OP's code *does* check for NULL, as you correctly advise. – Joseph Quinsey Feb 20 '13 at 02:19