-1

I will be short on what is the problem:

I created an array of strings using malloc. Now, I want to put sentences from a txt file into these strings. I do not get any error, but when I want to print out the strings after the "read-in", it is just blank, there are no sentences at all.

I link the code of the program related to this.

Where did I make a mistake?

Please help.

Thanks!

EDIT: I got the problem.

questions[i]=(char*) malloc(sizeof(char));

Is only allocates 1 byte. The question now is the following: HOW should I allocate more bytes then? These 'question[i]'s should be as 'long' as the sentence in it, said the teacher, but I do not know how to accomplish that.

char** questions
int numbofquestions=40;

questions=(char**) malloc(sizeof(char*)*numbofquestions);
int i;
for(i=0;i<numbofquestions;i++)
{
 questions[i]=(char*) malloc(sizeof(char));
}

FILE* fp;
fp=fopen("sentences.txt", "r");
for(i=0;i<4;i++) // LESS THAN 4 BECAUSE IT IS JUST A TEST, THERE IS ONLY 4 SENTENCES IN THE FILE AT THE MOMENT. EACH SENTENCE IS IN A DIFFERENT ROW.
{
 fgets(questions[i],sizeof(char),fp);
 printf("%s\n", questions[i]);
}
fclose(fp);

free(questions);
for(i=0;i<numbofquestions;i++)
{
 free(questions[i]);
}
Noxter
  • 91
  • 1
  • 9
  • 2
    `questions[i]=(char*) malloc(sizeof(char));` allocates 1 byte. Seems a bit small. – chux - Reinstate Monica Nov 15 '16 at 14:24
  • 2
    `sizeof(char)` is `1`. So `fgets(questions[i],sizeof(char),fp);` hold one charactor (NUL terminator). `fgets` can't read from file. – BLUEPIXY Nov 15 '16 at 14:24
  • When given a single character buffer, `fgets()` can only store the null terminator. You need to allocate more space per string, or read the line into a big string and then duplicate (`strdup()` instead of the original `malloc()` is easiest; you could use `realloc()` since you already did `malloc()` for each string) the line into the stored string. – Jonathan Leffler Nov 15 '16 at 14:27
  • I assume your questions are more than 1 character long? (zero long actually) `questions[i]=(char*) malloc(sizeof(char));` would be wrong then. (You might actually do better find the size of the question file, `malloc`ing that, read the whole file into that buffer, then setting your pointers into that. It wouldn't be zero terminated strings. But you could clobber line-ends with zeros to get that) – infixed Nov 15 '16 at 14:29
  • Hey guys! Thanks all of you for your answers! I see, so I only allocated 1 byte there. That is indeed pretty bad. Although, can you guys tell me, how should i allocate more bytes then? I mean, the sentences have different lengths, so that is pretty problemastic. My teacher said that these 'questions[i]' -s should be as 'long' in bytes as the stored sentence in it. How should I accomplish that? – Noxter Nov 15 '16 at 14:41

1 Answers1

2

There are three mistakes.

malloc(sizeof(char))

,

fgets(questions[i],sizeof(char),fp);

and sequence of

free(...)


[1]:

int maxLengthOfString = 128; // or more.
...
(char*) malloc(sizeof(char) * maxLengthOfString);

because

sizeof(char) // == just 1 byte == 1 character == only '\0' in string.


[2]:

fgets(questions[i], sizeof(char) * maxLengthOfString, fp);

same reason as [1].


[3]:

for(i=0; i<numbofquestions; i++)
{
    free(questions[i]);
}
free(questions);

free(...) must in reverse order on this situation.

NeuroWhAI
  • 38
  • 6
  • Hey! Thank you for your answer! it helps! :) May I ask you, that if I do not know the length of all of my sentences obviously, then what 'maxLengthOfString' should be in order to be able to store all of my sentences regardless of their length? – Noxter Nov 15 '16 at 14:48
  • maxLengthOfString is just big enough. – NeuroWhAI Nov 15 '16 at 14:49
  • 1
    Tried it. it Works. I mark you as a solution. HUGE THANKS for the fast and correct answer! Have a nice day! – Noxter Nov 15 '16 at 14:55
  • ;) Have a nice day~ – NeuroWhAI Nov 15 '16 at 15:01
  • 1
    don't cast the result of malloc http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – plean Nov 15 '16 at 15:40