C 如何将文件中的字符放入动态数组

C 如何将文件中的字符放入动态数组,c,arrays,C,Arrays,我试图从file.txt文件中获取所有整数,并将它们放入动态分配的数组中。但是,文件也可能包含其他字符,这些字符不应放入数组中 该文件包含以下内容: 2-34 56-23424 12示例-34en+56ge-tal345 而正确的输出应该是 找到的号码: 代码中存在许多循环漏洞。您需要首先修复它 int c= fgetc(fileInput); while(c != EOF) { counter ++; printf("%d;\t%d\n", counter, c);fflush

我试图从file.txt文件中获取所有整数,并将它们放入动态分配的数组中。但是,文件也可能包含其他字符,这些字符不应放入数组中

该文件包含以下内容:

2-34 56-23424 12示例-34en+56ge-tal345

而正确的输出应该是

找到的号码:


代码中存在许多循环漏洞。您需要首先修复它

int c= fgetc(fileInput);
while(c != EOF)
{
    counter ++;
    printf("%d;\t%d\n", counter, c);fflush(stdout);
    temp[counter++] = c;
}
这个密码让我发疯。从文件中只读取一个字符并在循环时运行,您会得到什么?给机会读另一个字节

必须检查返回值
fopen
功能。如果文件不存在或打开时出错怎么办?你的节目疯了。所以确保

 FILE* fileInput = fopen(filename, "r");
 if(fileInput==NULL ){
  printf("Error to open file\n");
  return
  }
同样,您在循环中将计数器增加了两次,第一次是
counter++temp[counter++]=c管理数组索引是错误的


最重要的是,每个打开的文件都必须关闭。

代码中有许多循环漏洞。您需要首先修复它

int c= fgetc(fileInput);
while(c != EOF)
{
    counter ++;
    printf("%d;\t%d\n", counter, c);fflush(stdout);
    temp[counter++] = c;
}
这个密码让我发疯。从文件中只读取一个字符并在循环时运行,您会得到什么?给机会读另一个字节

必须检查返回值
fopen
功能。如果文件不存在或打开时出错怎么办?你的节目疯了。所以确保

 FILE* fileInput = fopen(filename, "r");
 if(fileInput==NULL ){
  printf("Error to open file\n");
  return
  }
同样,您在循环中将计数器增加了两次,第一次是
counter++temp[counter++]=c管理数组索引是错误的


另外,最重要的是每个打开的文件都必须关闭。

该程序有很多问题

  • 你泄漏了你的文件对象。完成后,应使用
    fclose()
    将其关闭
  • 如果输入中的数字超过100个,则会导致缓冲区溢出、堆栈垃圾,并对程序造成不良影响
  • 在每次循环迭代中,您将计数器增加两次,因此您将跳过输出数组中的每一秒条目
  • 您从未从输入中读取另一个字节,因此您只需在无限循环中反复处理同一字节,直到缓冲区溢出导致程序崩溃
  • 从不将从输入文件中读取的数字转换为整数;相反,您只需获取字符代码。49是“1”的ASCII/UTF-8代码,它似乎是输入中的第一个字符

这个程序有很多问题

  • 你泄漏了你的文件对象。完成后,应使用
    fclose()
    将其关闭
  • 如果输入中的数字超过100个,则会导致缓冲区溢出、堆栈垃圾,并对程序造成不良影响
  • 在每次循环迭代中,您将计数器增加两次,因此您将跳过输出数组中的每一秒条目
  • 您从未从输入中读取另一个字节,因此您只需在无限循环中反复处理同一字节,直到缓冲区溢出导致程序崩溃
  • 从不将从输入文件中读取的数字转换为整数;相反,您只需获取字符代码。49是“1”的ASCII/UTF-8代码,它似乎是输入中的第一个字符
试试这个

#include <stdlib.h>
#include <stdio.h>


int* getIntegers(char* filename, int* pn)
{
    int* temp = (int*)malloc( 100*sizeof(int));
    int counter = 0;
    int result;
    int c;

    FILE* fileInput = fopen(filename, "r");
    if ( fileInput == NULL) {
        return temp;  // return if file open fails
    }
    while( 1) {
        result = fscanf (fileInput, "%d", &c);//try to read an int
        if ( result == 1) { // read successful
            temp[counter] = c; //save int to array
            counter++;
            printf("%d;\t%d\n", counter, c);
        }
        else { // read not successful
            fscanf ( fileInput, "%*[^-+0-9]"); //scan for anything not a -, + or digit
        }
        if ( counter > 98) { // dont exceed array 
            break;
        }
        if ( feof( fileInput)) { // check if at end of file
            break;
        }
    }
    fclose ( fileInput); // close the file
    *pn = counter;
    return (temp);
}

int main(void)
{
    int n;
    int i;
    int* a = getIntegers("file.txt", &n);
    if (a != NULL){
    printf("numbers found:");
    for (i = 0;i < n; i++){
        printf("%d ",a[i]);
    }
    free(a);
    }
    putchar('\n');

    return (EXIT_SUCCESS);
}
#包括
#包括
int*getIntegers(字符*文件名,int*pn)
{
int*temp=(int*)malloc(100*sizeof(int));
int计数器=0;
int结果;
INTC;
FILE*fileInput=fopen(文件名,“r”);
if(fileInput==NULL){
return temp;//文件打开失败时返回
}
而(1){
result=fscanf(fileInput,'%d',&c);//尝试读取一个int
如果(结果==1){//读取成功
temp[counter]=c;//将int保存到数组
计数器++;
printf(“%d;\t%d\n”,计数器,c);
}
否则{//读取不成功
fscanf(fileInput,“%*[^-+0-9]”;//扫描除-、+或数字以外的任何内容
}
如果(计数器>98){//不超过数组
打破
}
if(feof(fileInput)){//检查文件末尾的if
打破
}
}
fclose(fileInput);//关闭文件
*pn=计数器;
返回(临时);
}
内部主(空)
{
int n;
int i;
int*a=getIntegers(“file.txt”,&n);
如果(a!=NULL){
printf(“找到的数字:”);
对于(i=0;i
试试这个

#include <stdlib.h>
#include <stdio.h>


int* getIntegers(char* filename, int* pn)
{
    int* temp = (int*)malloc( 100*sizeof(int));
    int counter = 0;
    int result;
    int c;

    FILE* fileInput = fopen(filename, "r");
    if ( fileInput == NULL) {
        return temp;  // return if file open fails
    }
    while( 1) {
        result = fscanf (fileInput, "%d", &c);//try to read an int
        if ( result == 1) { // read successful
            temp[counter] = c; //save int to array
            counter++;
            printf("%d;\t%d\n", counter, c);
        }
        else { // read not successful
            fscanf ( fileInput, "%*[^-+0-9]"); //scan for anything not a -, + or digit
        }
        if ( counter > 98) { // dont exceed array 
            break;
        }
        if ( feof( fileInput)) { // check if at end of file
            break;
        }
    }
    fclose ( fileInput); // close the file
    *pn = counter;
    return (temp);
}

int main(void)
{
    int n;
    int i;
    int* a = getIntegers("file.txt", &n);
    if (a != NULL){
    printf("numbers found:");
    for (i = 0;i < n; i++){
        printf("%d ",a[i]);
    }
    free(a);
    }
    putchar('\n');

    return (EXIT_SUCCESS);
}
#包括
#包括
int*getIntegers(字符*文件名,int*pn)
{
int*temp=(int*)malloc(100*sizeof(int));
int计数器=0;
int结果;
INTC;
FILE*fileInput=fopen(文件名,“r”);
if(fileInput==NULL){
return temp;//文件打开失败时返回
}
而(1){
result=fscanf(fileInput,'%d',&c);//尝试读取一个int
如果(结果==1){//读取成功
temp[counter]=c;//将int保存到数组
计数器++;
printf(“%d;\t%d\n”,计数器,c);
}
否则{//读取不成功
fscanf(fileInput,“%*[^-+0-9]”;//扫描除-、+或数字以外的任何内容
}
如果(计数器>98){//不超过数组
打破
}
if(feof(fileInput)){//检查文件末尾的if
打破
}
}
fclose(fileInput);//关闭文件
*pn=计数器;
返回(临时);
}
内部主(空)
{
int n;
int i;
int*a=getIntegers(“file.txt”,&n);
如果(a!=NULL){
printf(“找到的数字:”);
对于(i=0;i
why
c=fgetc(fileInput)一次?并且不应该在循环中增加计数器两次。为什么
c=fgetc(fileInput)一次?并且您不应该在l中增加计数器两次