我想将input.txt文件更改为整数数组。 但是可悲的是,每当遇到换行符时,我总是会丢失一个整数。
以下是我的main()
int main(int args, char* argv[]) {
int *val;
char *STRING = readFile();
val = convert(STRING);
return 0;
}
以下是我的文件输入功能
char *readFile() {
int count;
FILE *fp;
fp = fopen("input.txt", "r");
if(fp==NULL) printf("File is NULL!n");
char* STRING;
char oneLine[255];
STRING = (char*)malloc(255);
assert(STRING!=NULL);
while(1){
fgets(oneLine, 255, fp);
count += strlen(oneLine);
STRING = (char*)realloc(STRING, count+1);
strcat(STRING, oneLine);
if(feof(fp)) break;
}
fclose(fp);
return STRING;
}
以下是我的整数数组函数
int *convert(char *STRING){
int *intarr;
intarr = (int*)malloc(sizeof(int)*16);
int a=0;
char *ptr = strtok(STRING, " ");
while (ptr != NULL){
intarr[a] = atoi(ptr);
printf("number = %s\tindex = %d\n", ptr, a);
a++;
ptr = strtok(NULL, " ");
}
return intarr;
}
答案 0 :(得分:1)
有很多问题。
这是您程序的更正版本,所有注释均为我的。为简便起见,进行了最小限度的错误检查。如果文件中包含16个以上的数字,<properties>
<wagon.url>cifs://dedcdevfs01/Seepub/QS</wagon.url>
<wagon.fromDir>manasa-utility/</wagon.fromDir>
<wagon.toDir>target/resources/testdata/</wagon.toDir>
</properties>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>downloadFilesInShare</id>
<phase>compile</phase>
<goals>
<goal>download</goal>
</goals>
<configuration>
<serverId>${wagon.serverId}</serverId>
<url>${wagon.url}</url>
<skip>${wagon.skip}</skip>
<fromDir>${wagon.fromDir}</fromDir>
<toDir>${wagon.toDir}</toDir>
</configuration>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
将是一个问题,应该以某种方式处理它,例如,通过将intarr = malloc(sizeof(int) * 16);
与intarr
一起增长,类似于您在进行的操作realloc
。
readFile
我不确定您的要求是什么,但这可以简化很多,因为不需要将文件读入内存,然后将字符串转换为数字。您可以在阅读数字时即时转换数字。并且正如注释中已经提到的那样,为每一行调用#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
char *readFile() {
FILE *fp;
fp = fopen("input.txt", "r");
if (fp == NULL)
{
printf("File is NULL!n");
return NULL; // abort if file could not be opened
}
#define MAXLINELENGTH 255 // define a constant rather than hardcoding "255" at several places
char* STRING;
char oneLine[MAXLINELENGTH];
STRING = malloc(MAXLINELENGTH);
int count = MAXLINELENGTH; // count mus be initialized and better declare it here
assert(STRING != NULL);
STRING[0] = 0; // memory pointed by STRING must be initialized
while (fgets(oneLine, MAXLINELENGTH, fp) != NULL) // correct usage of fgets
{
count += strlen(oneLine);
STRING = realloc(STRING, count + 1);
strcat(STRING, oneLine);
}
fclose(fp);
return STRING;
}
int *convert(char *STRING, int *nbofvalues) { // nbofvalues for returning the number of values
int *intarr;
intarr = malloc(sizeof(int) * 16);
int a = 0;
char *ptr = strtok(STRING, " \n"); // strings may be separated by '\n', or ' '
*nbofvalues = 0;
while (ptr != NULL) {
intarr[a] = atoi(ptr);
printf("number = %s\tindex = %d\n", ptr, a);
a++;
ptr = strtok(NULL, " \n"); // strings are separated by '\n' or ' '
} // read the fgets documentation which
// terminates read strings by \n
*nbofvalues = a; // return number of values
return intarr;
}
int main(int args, char* argv[]) {
int *val;
char *STRING = readFile();
if (STRING == NULL)
{
printf("readFile() problem\n"); // abort if file could not be read
return 1;
}
int nbvalues;
val = convert(STRING, &nbvalues); // nbvalues contains the number of values
// print numbers
for (int i = 0; i < nbvalues; i++)
{
printf("%d: %d\n", i, val[i]);
}
free(val); // free memory
free(STRING); // free memory
return 0;
}
效率很低。还有更多改进的空间。