0

Why doesn't this main print anything? It should print the first word in the file.

int main(int argc, char *argv[])
{
  FILE *file = fopen(argv[1], "r");
  int n = atoi(argv[2]);

  char **words = new char*[n];
  for(int i = 0; i < n; i++)
    {
      fscanf(file, "%s ", words[i]);
    }
  cout << words[0] << endl;
}
2
  • 1
    Apart from lack of error checking, you also have undefined behaviour reading data into arbitrary memory via uninitialised pointers. Commented Feb 3, 2016 at 2:49
  • Except for the output stream, this is just C.... Commented Feb 3, 2016 at 7:06

2 Answers 2

2

char **words = new char*[n]; will allocate a buffer to hold n pointers to char, words is just a pointer to an array of pointers. You need allocate enough memory for words[i](the pointer to ) to hold the string:

for (i=0; i < n; ++i ) {
    words[i] = new char[your_max_string_len];
}

Optionally you can use getline extension of GNU(if you use gcc) to do what you want:

size_t len = 0;
ssize_t read;
read = getline(&words[i], &len, stdin);
...
free(words[i]);

Actually, there is no magic in this function, it just does the memory allocation under the hood to save yours, and it's your responsibility to free it.

Sign up to request clarification or add additional context in comments.

7 Comments

What if I don't know how many bytes the words will need? How can I look at each word, see how many bytes it needs, and then allocate that many bytes? Would I use a temp variable?
If you don't read the string, how can you know the length of the string? But you need allocate enough memory when you read the string:)
So I would read it into a temp variable, then look at the length of the string in the temp variable, then copy it into the array?
Yes, you can use a char[your_max_string_len] as a temp variable to read the string ,the strcpy to the allocated memory.
Optionally you can use getline extension of GNU to do what you want: read = getline(&words[i], &len, stdin)
|
2

words[i] is a pointer that points to a random memory location. Make sure to make it point to allocated memory.

//Now words[i] points to the 1000 bytes allocated with the new keyword
words[i] = new char[1000];
//fscan will write the input to those bytes
fscanf(file, "%s ", words[i]);

2 Comments

What if I don't know how many bytes the words will need? How can I look at each word, see how many bytes it needs, and then allocate that many bytes? Would I use a temp variable?
To figure out how big the line you are reading is, you must read it until you reach the line terminator. One way of doing this is by allocating a large memory buffer, reading the data into it, and checking if the line terminator was found. If it wasn't, realloc a larger buffer, read more data into it, and check again. Here is a example: stackoverflow.com/questions/2532425/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.