Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取文件,只获取整数,并一直读到最后 #包括 int getIntegers(char*filename,inta[]); 内部主(空){ ///// 文件*fp; char文件[10]=“random.txt”; fp=fopen(文件“w”); fprintf(fp,“12-34 56-98 42516547示例-34t+56ge-pad12345\n”); fclose(fp); ///// int i; INTA[100]; int n=getIntegers(文件,a); //在这里,我想打印出我从getIntegers得到的结果。它应该输出什么=“12-3456-9842516547-345612345” 如果(n>0) { puts(“找到的数字:”); 对于(i=0;i_C_File - Fatal编程技术网

读取文件,只获取整数,并一直读到最后 #包括 int getIntegers(char*filename,inta[]); 内部主(空){ ///// 文件*fp; char文件[10]=“random.txt”; fp=fopen(文件“w”); fprintf(fp,“12-34 56-98 42516547示例-34t+56ge-pad12345\n”); fclose(fp); ///// int i; INTA[100]; int n=getIntegers(文件,a); //在这里,我想打印出我从getIntegers得到的结果。它应该输出什么=“12-3456-9842516547-345612345” 如果(n>0) { puts(“找到的数字:”); 对于(i=0;i

读取文件,只获取整数,并一直读到最后 #包括 int getIntegers(char*filename,inta[]); 内部主(空){ ///// 文件*fp; char文件[10]=“random.txt”; fp=fopen(文件“w”); fprintf(fp,“12-34 56-98 42516547示例-34t+56ge-pad12345\n”); fclose(fp); ///// int i; INTA[100]; int n=getIntegers(文件,a); //在这里,我想打印出我从getIntegers得到的结果。它应该输出什么=“12-3456-9842516547-345612345” 如果(n>0) { puts(“找到的数字:”); 对于(i=0;i,c,file,C,File,我有一个文件,里面有数字和单词/字母。通过这段代码,我得到整数直到第一个字母,但我想继续到EOF。然后返回这些数字并在main中打印出来。我试过了,但没能成功。我应该/可以做些什么来让它工作?或者我做错了什么。多个问题: int getIntegers()不返回任何值。代码不会在a[]中保存任何内容。未强制执行数组限制 注释代码不检查fscanf()的返回值 当fscanf()返回0时,代码需要使用1个字符,然后重试。 当fscanf(fp,“%d”,&a[i])返回0时,表示输入不是数字,并且

我有一个文件,里面有数字和单词/字母。通过这段代码,我得到整数直到第一个字母,但我想继续到EOF。然后返回这些数字并在main中打印出来。我试过了,但没能成功。我应该/可以做些什么来让它工作?或者我做错了什么。

多个问题:

int getIntegers()
不返回任何值。代码不会在a[]中保存任何内容。未强制执行数组限制

注释代码不检查
fscanf()
的返回值

fscanf()返回0时,代码需要使用1个字符,然后重试。

fscanf(fp,“%d”,&a[i])返回0时,表示输入不是数字,并且
fscanf()
没有使用任何非数字输入。因此,请阅读1个字符,然后重试

#include <stdio.h>


int getIntegers(char *filename,int a[]);

int main(void) {
    /////
    FILE *fp;
    char file[10] = "random.txt";
    fp = fopen(file, "w");
    fprintf(fp, "1 2 -34 56 -98 42516547example-34t+56ge-pad12345\n");
    fclose(fp);
    /////

    int i;

    int a[100]; 
    int n = getIntegers(file,a);
    //Here i want to print out what i got from getIntegers. What it should put out = "1 2 -34 56 -98 42516547 -34 56 12345"
    if (n > 0) 
    {
        puts("found numbers:");
        for(i = 0;i < n; i++)
            {
                printf("%d ",a[i]);    
            }
        putchar('\n');
    }
    return 0;
}

int getIntegers(char *filename, int a[])
{
    int c, i;
    FILE *fp;
    fp = fopen(filename, "r");
//I want what this code does to be done with the commented code under it. This will give "1 2 -34 56 -98 42516547"
    while (fscanf(fp,"%d",&i)==1) 
    {
        printf("%d ",i);
    }
    fclose(fp);

// I want this code to give  "1 2 -34 56 -98 42516547 -34 56 12345"  
//    while ((c = fgetc(fp)) != EOF) 
//    {           
//        for(i = 0; i < c;i++)
//        {
//            fscanf(fp, "%1d", &a[i]);
//        }
//    }
//    return i;
}
多个问题:

int getIntegers()
不返回任何值。代码不会在a[]中保存任何内容。未强制执行数组限制

注释代码不检查
fscanf()
的返回值

fscanf()返回0时,代码需要使用1个字符,然后重试。

fscanf(fp,“%d”,&a[i])返回0时,表示输入不是数字,并且
fscanf()
没有使用任何非数字输入。因此,请阅读1个字符,然后重试

#include <stdio.h>


int getIntegers(char *filename,int a[]);

int main(void) {
    /////
    FILE *fp;
    char file[10] = "random.txt";
    fp = fopen(file, "w");
    fprintf(fp, "1 2 -34 56 -98 42516547example-34t+56ge-pad12345\n");
    fclose(fp);
    /////

    int i;

    int a[100]; 
    int n = getIntegers(file,a);
    //Here i want to print out what i got from getIntegers. What it should put out = "1 2 -34 56 -98 42516547 -34 56 12345"
    if (n > 0) 
    {
        puts("found numbers:");
        for(i = 0;i < n; i++)
            {
                printf("%d ",a[i]);    
            }
        putchar('\n');
    }
    return 0;
}

int getIntegers(char *filename, int a[])
{
    int c, i;
    FILE *fp;
    fp = fopen(filename, "r");
//I want what this code does to be done with the commented code under it. This will give "1 2 -34 56 -98 42516547"
    while (fscanf(fp,"%d",&i)==1) 
    {
        printf("%d ",i);
    }
    fclose(fp);

// I want this code to give  "1 2 -34 56 -98 42516547 -34 56 12345"  
//    while ((c = fgetc(fp)) != EOF) 
//    {           
//        for(i = 0; i < c;i++)
//        {
//            fscanf(fp, "%1d", &a[i]);
//        }
//    }
//    return i;
}

int getIntegers()
不返回任何值。代码不会在a[]中保存任何内容int getIntegers()不会返回任何值。代码不会在a[]中保存任何内容。感谢您解释我遇到的问题。考虑到这一点,它帮助我更好地理解。谢谢你解释我遇到的问题。考虑到这一点,它帮助我更好地理解。
found numbers:1 2 -34 56 -98 42516547 -34 56 12345