I am trying to read input from a file and put each string in an array using malloc and realloc. So if the input file is :
我试图从文件读取输入并使用malloc和realloc将每个字符串放在一个数组中。所以如果输入文件是:
alex
john
jane
smith
the content of array contain {"alex\n", "john\n", "jane\n", "smith\n"}. So far I have done something like this:
数组的内容包含{“alex \ n”,“john \ n”,“jane \ n”,“smith \ n”}。到目前为止,我做过这样的事情:
int n=0;
int size=1;
File *fp = fopen(args[0],"r");
int c;
char* inputFile;
inputFile = (char*) malloc(size);
if(fp==0){
fprintf(stderr, "Cannot open file!\n");
return -1;}
else{
do{
c = fgetc(fp);
inputFile = (char*) realloc(inputFile, size+1);
inputFile[n]=c;
n++;
size++;
}while(c!=EOF);
I believe this algorithm will end up with array like this {'a','l','e','x','\n','j','o','h','n','\n','j','a','n','e','\n','s','m','i','t','h','\n'}
我相信这个算法最终会得到像这样的数组{'a','l','e','x','\ n','j','o','h','n',' \ n”, 'J', 'A', 'N', 'E', '\ n', 'S', 'M', 'I', 'T', 'H', '\ n'}
how can I make the inputFile become 2 dimension array? what should I do with realloc?
如何使inputFile成为二维数组?我应该怎么做realloc?
2 个解决方案
#1
0
You can try the following or just study how it's done. It works fine on my linux machine. Let me know if you have any questions.
您可以尝试以下方法或只研究它是如何完成的。它在我的linux机器上工作正常。如果您有任何疑问,请告诉我。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int INITIAL_MAX_LINES = 2;
const int MAX_LINES_INC = 2;
const int INITIAL_MAX_LINE_LENGTH = 2;
const int MAX_LINE_LENGTH_INC = 2;
int main (int argc, char *argv[])
{
int nlines = 0, i;
FILE *fp = fopen(argv[1], "r");
char **inputFile, *buffer;
int max_lines, c, buflen, bufpos, end_of_line;
if (argc < 2) {
printf("No enough arguments.\n");
return -1;
}
max_lines = INITIAL_MAX_LINES;
inputFile = (char **) malloc(max_lines * sizeof(char*));
if (fp==0) {
fprintf(stderr, "Cannot open file!\n");
return -1;
}
else{
/* Start with a buffer. */
bufpos = 0;
buflen = INITIAL_MAX_LINE_LENGTH;
buffer = (char *) malloc(buflen * sizeof(char *));
c = 0;
while (c != EOF) {
end_of_line = 0;
c = fgetc(fp);
if (c == EOF || c == '\n' || c == '\r') {
end_of_line = 1;
/* Discard this character. */
}
else {
/* Put this character in the buffer. */
/* But check if we have enough memory first! */
/* Leave room for the null character at the end. */
if (bufpos >= buflen - 1) {
buflen += MAX_LINE_LENGTH_INC;
buffer = (char *) realloc(buffer, buflen * sizeof(char));
}
buffer[bufpos] = c;
bufpos++;
}
if (end_of_line) {
/* Remember this line and get a new buffer. */
/* Check if we need more memory. */
if (nlines >= max_lines) {
max_lines += MAX_LINES_INC;
inputFile = (char **) realloc(inputFile, max_lines * sizeof(char*));
}
/* Null terminate the buffer.*/
buffer[bufpos++] = 0;
inputFile[nlines] = buffer;
nlines++;
bufpos = 0;
buflen = INITIAL_MAX_LINE_LENGTH;
buffer = (char *) malloc(buflen * sizeof(char *));
}
}
}
printf("%d lines\n", nlines);
for (i=0; i<nlines; i++) {
printf("%s\n", inputFile[i]);
}
}
#2
0
you would want to allocate an array of pointers first with an initial size say 10:
你想要首先分配一个指针数组,初始大小为10:
int size = 10;
char **inputFile= malloc(sizeof*inputFile*size);
Then for each word you read you allocate more memory for it and insert it into the array:
然后,对于您阅读的每个单词,为其分配更多内存并将其插入到数组中:
char line[100];
fscanf(file, "%s", line);
inputFile[index++] = strdup(line);
Now check if you need more words then you realloc the array:
现在检查是否需要更多单词然后重新分配数组:
if (index==size) {
size += 10;
inputFile = realloc(inputFile, sizeof*inputFile*size);
}
So you end up with something like this:
所以你最终得到这样的东西:
[0]->"alex"
[1]->"john"
[2]->"jane"
[3]->"smith"
When you're done you need to loop over the array and free each string and then free the array, this part is left as an exercise :)
当你完成后,你需要循环数组并释放每个字符串然后释放数组,这部分留作练习:)
#1
0
You can try the following or just study how it's done. It works fine on my linux machine. Let me know if you have any questions.
您可以尝试以下方法或只研究它是如何完成的。它在我的linux机器上工作正常。如果您有任何疑问,请告诉我。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int INITIAL_MAX_LINES = 2;
const int MAX_LINES_INC = 2;
const int INITIAL_MAX_LINE_LENGTH = 2;
const int MAX_LINE_LENGTH_INC = 2;
int main (int argc, char *argv[])
{
int nlines = 0, i;
FILE *fp = fopen(argv[1], "r");
char **inputFile, *buffer;
int max_lines, c, buflen, bufpos, end_of_line;
if (argc < 2) {
printf("No enough arguments.\n");
return -1;
}
max_lines = INITIAL_MAX_LINES;
inputFile = (char **) malloc(max_lines * sizeof(char*));
if (fp==0) {
fprintf(stderr, "Cannot open file!\n");
return -1;
}
else{
/* Start with a buffer. */
bufpos = 0;
buflen = INITIAL_MAX_LINE_LENGTH;
buffer = (char *) malloc(buflen * sizeof(char *));
c = 0;
while (c != EOF) {
end_of_line = 0;
c = fgetc(fp);
if (c == EOF || c == '\n' || c == '\r') {
end_of_line = 1;
/* Discard this character. */
}
else {
/* Put this character in the buffer. */
/* But check if we have enough memory first! */
/* Leave room for the null character at the end. */
if (bufpos >= buflen - 1) {
buflen += MAX_LINE_LENGTH_INC;
buffer = (char *) realloc(buffer, buflen * sizeof(char));
}
buffer[bufpos] = c;
bufpos++;
}
if (end_of_line) {
/* Remember this line and get a new buffer. */
/* Check if we need more memory. */
if (nlines >= max_lines) {
max_lines += MAX_LINES_INC;
inputFile = (char **) realloc(inputFile, max_lines * sizeof(char*));
}
/* Null terminate the buffer.*/
buffer[bufpos++] = 0;
inputFile[nlines] = buffer;
nlines++;
bufpos = 0;
buflen = INITIAL_MAX_LINE_LENGTH;
buffer = (char *) malloc(buflen * sizeof(char *));
}
}
}
printf("%d lines\n", nlines);
for (i=0; i<nlines; i++) {
printf("%s\n", inputFile[i]);
}
}
#2
0
you would want to allocate an array of pointers first with an initial size say 10:
你想要首先分配一个指针数组,初始大小为10:
int size = 10;
char **inputFile= malloc(sizeof*inputFile*size);
Then for each word you read you allocate more memory for it and insert it into the array:
然后,对于您阅读的每个单词,为其分配更多内存并将其插入到数组中:
char line[100];
fscanf(file, "%s", line);
inputFile[index++] = strdup(line);
Now check if you need more words then you realloc the array:
现在检查是否需要更多单词然后重新分配数组:
if (index==size) {
size += 10;
inputFile = realloc(inputFile, sizeof*inputFile*size);
}
So you end up with something like this:
所以你最终得到这样的东西:
[0]->"alex"
[1]->"john"
[2]->"jane"
[3]->"smith"
When you're done you need to loop over the array and free each string and then free the array, this part is left as an exercise :)
当你完成后,你需要循环数组并释放每个字符串然后释放数组,这部分留作练习:)