![](/img/trans.png)
[英]How do i read from a text file and allocate memory space using malloc for a 2d string array for each word in the text file
[英]How do I read input as a string and store each word (a word is anything separated by a space) to a 2d array using malloc, strtok_r?
我是 C 的新手,我知道某些對此有幫助的函數,但是我很難將事情拼湊起來。
到目前為止我使用的過程:使用readline獲取輸入,使用strtok_r計算單詞數,單詞的malloced空間,每個單詞中的計數器字符和malloced空間,然后打印數組。
但是,在運行此程序時,出現段錯誤。
while(inStr = readline("# "))
{
int totalWords = totalWordsInInput(inStr);
//malloc the rows (array of pointer-total number of words)
char** theBigArray = (char**) malloc((totalWords+1)*(sizeof(char*))); //+1 so we can save a null
for(int i = 0; i <= totalWords; i++)
{
theBigArray[i] = NULL;
}
//malloc for each word
char* ptr = inStr;
char* saveptr;
for(int i = 0; i < totalWords; i++, ptr = NULL) //for every word
{
int totalChar = strlen((strtok_r(ptr, " ", &saveptr)));
theBigArray[i] = (char*) malloc((totalChar+1)*(sizeof(char)));
for(int j = 0; j <= totalChar; j++)
{
theBigArray[i][j] = '\0';
}
}
//populate the malloced array
char* ptr1 = inStr;
char* buffer1;
int k = 0;
for(int i = 0; (i < totalWords) && (buffer1 = strtok_r(ptr1, " ", &ptr1)); i++)
{
theBigArray[i] = buffer1;
}
//printing the malloced array
for(int i = 0; i < totalWords; i++)
{
printf("%s", theBigArray[i]);
}
}
int totalWordsInInput(char* inputString)
{
int counterWords = 0;
char* buffer;
char* ptr = inputString;
while(buffer = strtok_r(ptr, " ", &ptr))
{
counterWords++;
}
return counterWords;
}
您的代碼中存在一些問題:
這是工作代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100
int totalWordsInInput(char *);
void getError(char *);
int main(int argc, const char * argv[]) {
char *text = (char *) malloc(MAX_LEN);
if (!text) getError("Input text");
printf("\nInsert some words: ");
fgets(text, MAX_LEN - 1, stdin);
text[strcspn(text, "\n")] = '\0';
text = (char *) realloc(text, strlen(text) + 1);
if (!text) getError("Input text");
int totalWords = totalWordsInInput(text);
char **bigArray = (char **) malloc(totalWords * sizeof(char *));
if (!bigArray) getError("big Array");
char *currentWord, *savePtr = NULL;
int i = 0;
for (currentWord = strtok_r(text, " ", &savePtr); currentWord != NULL; currentWord = strtok_r(NULL, " ", &savePtr)) {
int totalChar = (int) strlen(currentWord);
bigArray[i] = (char *) malloc(totalChar + 1);
if (!bigArray[i]) getError("bigArray[i]");
strcpy(bigArray[i], currentWord);
bigArray[i][totalChar + 1] = '\0';
i++;
}
printf("\nSingle words:\n");
for(int i = 0; i < totalWords; i++) printf("%s\n", bigArray[i]);
printf("\n\n\n");
return 0;
}
int totalWordsInInput(char *inputString) {
int counterWords = 0;
char *text = (char *) malloc(strlen(inputString) + 1);
if (!text) getError("total Words In Inpu Text");
strcpy(text, inputString);
char *word, *savePtr = NULL;
for (word = strtok_r(text, " ", &savePtr); word != NULL; word = strtok_r(NULL, " ", &savePtr)) {
counterWords++;
}
return counterWords;
}
void getError(char *errorMessage) {
printf("\nError allocating memory for %s...", errorMessage);
exit(EXIT_FAILURE);
}
個人提示:
我希望它至少對你有一點幫助。
最好的問候,丹尼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.