views:

180

answers:

4

How do I put strings into an 2D char array from (for example) a file?

char buffert[10][30];
int i = 0;

while(!feof(somefile)) {
  fscanf(somefile, "%s", temp);
  buffert[i][] = temp;
  i++;
}

This will not do it.

+3  A: 

Try this:

while(i < 10 && fscanf(somefile, "%29s%*[^ \t\n]", buffer[i]) != EOF) {
  i++;
}

The "%29s" format specifier reads up to 29 characters into buffer[i], which is all it has space for. The "%*[^ \t\n]" reads and throws away non-whitespace, in case the string was longer than 29 characters.

Additionally, while (!feof()) { } is almost always the wrong thing to do, because EOF isn't set on the stream until end of file is encountered.

caf
Ok, great answer I will check on this more advanced stuff with built-in check later.
Chris_45
A: 

Stolen from this forum:

#include <stdio.h>

main()
{
  int dirsize = 80;
  char array[9][dirsize];
  int linenum = 0;
  FILE *filepointer;
  filepointer = fopen("projectfile.txt","r");
  while (feof(filepointer) == 0)
  {
    linenum++;
    fgets(array[linenum], dirsize, filepointer);
  }
  fclose(filepointer);
}

Another solution with a pointer to arrays instead of array of arrays is here.

DVK
A: 

Do away with the temp, and drop each string straight into the space you created for it (in buffert in this case).

char buffert[10][30];
int i = 0;
FILE * fp = fopen("myfile", "r");
while(!feof(fp)) { 
  fscanf(fp, "%s", buffert[i]);
  i++;
}

Here the file pointer is called fp. You will have to make a bunch of checks to prevent overflowing the 10 entries available in buffert, or the 30 chars available in each of its strings. You should also avoid feof().

Ewan Todd
This has two problems: An overlong long string, or too many strings, will both overflow `buffert`; and when end-of-file is reached, `i` will be incremented one too many times, because the return value of `fscanf` isn't tested.
caf
I agree, which is why I provide a caveat about checks. However, I want to supply the simplest variation that demonstrates the point that the OP needs. It's a pedagogical thing.
Ewan Todd
-1 See http://c-faq.com/stdio/feof.html
Sinan Ünür
@Sinan Ünür: Ouch! My point wasn't about feof() at all. Do I have to nit-pick the OP's question to avoid getting stung?
Ewan Todd
@Ewan Todd: The `feof` bug is not trivial and pointing it out is not nitpicking. Of course, you could fix your answer instead of helping perpetuate buggy code in which case my downvote would be reversed.
Sinan Ünür
@Ewan Todd, I don't think it's about avoiding `feof()`, I think it's about using it correctly.
mrduclaw
If the OP is trying to cut down a tree whilst standing on a wheely chair balanced precariously on the roof of a car without the handbrake applied, and is having trouble starting the chainsaw - the best response isn't to just start the chainsaw for him and continue on your way.
caf
A: 

it doesn't look to me like you're actually COPYING the string into an array. I'm surprised it compiles. am I missing something?

look into strcpy or strncpy, or you can copy using for loops.

San Jacinto