如何用c语言一个一个只读取文档中字符串的数字

如图写到一半就卡住了,不能从文档里读取数字,运行后打印不出来,求指点

img

如果是想读取一个完整的整数如下

 FILE*fin;
 int _eof;
 fin=fopen("Filel.txt","r");
 if (!fin) { printf("open file error\n"); return ; } //判断是否成功打开文件
 printf("The file Eilel txt contained these integer values:\n")
 _eof = fscanf(fin,"%d", &text): //读取数据,如果结束或者错误 _eof = EOF
 while(_eof != EOF){
    printf("%d ",text);
    _eof = fscanf(fin,"%d", &text): //fscanf读取遇到空格和回车结束一次读写
 }
 printf("\n");
 fclose(fin);

%d改成%c啊。你是读数字字符,要读出字符后判断是不是数字。不能%d直接读取所有整数的,遇到字母等其它字符跳不过去,死循环
另外fprintf是写入文件,不是输出到显示窗口。要用printf

 FILE* fin;
 fin=fopen("File1.txt","r");
 printf("The file File1 txt contained these integer values:\n");
 char ch;
 int n=0;
 while((ch=fgetc(fin)) != '\n')
 {
    if(ch >='0' && ch <='9')
    {
       printf("%c",ch);
       n++;
    }
    else if(n>0)
    {
       printf(" ");
       n=0;
    } 
 }
 fclose(fin);

这是核心代码

char ch;
while( (ch=fgetc(fp)) != EOF ){
    if (ch>='0' && ch<='9'){
        printf("%c", ch);
    }
 }

可以学习下这篇博文【C语言读取字符串中的数字】,对你编写该需求有所帮助:https://www.csdn.net/tags/NtDaQgxsMDI5NjMtYmxvZwO0O0OO0O0O.html


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//#include <stdlib.h>

int main()
{
//下面是读数据,将读到的数据存到数组a[10]中,并且打印到控制台上
    int i = 0, j = 0, k = 0;
    char a[17500] ;//= { 0 }
    char b[5000] = { 0 };

    FILE* fpRead = fopen("要读取的文件路径", "r");
    if (fpRead == NULL)
    {
        return 0;
    }
    for ( i = 0; i < 17500; i++)
    {
        fscanf(fpRead, "%c ", &a[i]);
        if (k == 3)
           {
               b[j] = ' ';
               k = 0;
               j++;
           }
        //printf("%c ", a[i]);
        if (a[i] <= '9')
        {
            if('0' <= a[i])
            {
                b[j] = a[i];
                k++;
                j++;
            }        
        }    
    }
//下面是写数据,将数字0~9写入到某某.txt文件中
    FILE* fpWrite = fopen("存放的文件路径", "w");
    if (fpWrite == NULL)
    {
        return 0;
    }
    for (i = 0; i < 5000; i++)
        fprintf(fpWrite, "%c", b[i]);
    fclose(fpWrite);

    /*for (i = 0; i < 500; i++)
    {
        printf("%c", );
    }*/
    return 1;
}

试一下下面的代码


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//#include <stdlib.h>

int main()
{

    int i = 0, j = 0, k = 0;
    char a[17500] ;//= { 0 }
    char b[5000] = { 0 };

    FILE* fpRead = fopen("Test.txt", "r");
    if (fpRead == NULL)
    {
        return 0;
    }
    for ( i = 0; i < 17500; i++)
    {
        fscanf(fpRead, "%c ", &a[i]);
        if (k == 3)
           {
               b[j] = ' ';
               k = 0;
               j++;
           }        
        if (a[i] <= '9')
        {
            if('0' <= a[i])
            {
                b[j] = a[i];
                k++;
                j++;
            }        
        }    
    }
   //将读取的数据写入newdata.txt
    FILE* fpWrite = fopen("newdata.txt", "w");
    if (fpWrite == NULL)
    {
        return 0;
    }
    for (i = 0; i < 5000; i++)
        fprintf(fpWrite, "%c", b[i]);
    fclose(fpWrite);    
    return 1;
}