请大家帮忙看看,我自己编译了还是没成功?!

读取config.txt文件的数据,内容为:
cfsetispeed=57600
stopbits=1  
parity=0  
databits=8  
要求c文件能读取到这4个变量,后输出这4个变量验证







慕的地8271018
浏览 94回答 2
2回答

猛跑小猪

自己对比下吧...............#include<stdio.h>void main(){FILE *fp = NULL;int number[4] = {};if (fp = fopen("e:\\1.txt", "r")){for (int i = 0; i < 4; i++)fscanf(fp, "%*[^=]=%d", number+i);fclose(fp);printf("%d, %d, %d, %d\n", number[0], number[1], number[2], number[3]);}return;}..................................................................最主要的原因,你那个for里面虽然有用 i 来控制次数,可fgets和sscanf里面根本就没用 i ,一直在往同一地址写数据,其他细节我就不多说了.....

手掌心

在C语言中,文件操作都是由库函数来完成的。要读取一个txt文件,首先要使用文件打开函数fopen()。fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式) 其中,“文件指针名”必须是被说明为FILE 类型的指针变量,“文件名”是被打开文件的文件名。 “使用文件方式”是指文件的类型和操作要求。“文件名”是字符串常量或字符串数组。其次,使用文件读写函数读取文件。在C语言中提供了多种文件读写的函数:&nbsp;·字符读写函数 :fgetc和fputc·字符串读写函数:fgets和fputs·数据块读写函数:freed和fwrite·格式化读写函数:fscanf和fprinf最后,在文件读取结束要使用文件关闭函数fclose()关闭文件。下面以格式化读写函数fscanf和fprintf为例,实现对文件A.txt(各项信息以空格分割)的读取,并将它的信息以新的格式(用制表符分割各项信息)写入B.txt,实现对A.txt的处理。C语言源程序如下所示:#include <stdio.h>#include <stdlib.h>#include <assert.h>typedef struct student{char name[32];int no;char sex[16];float score;} stu;int main(int argc, char* argv[]){//打开文件&nbsp;FILE * r=fopen("A.txt","r");assert(r!=NULL);FILE * w=fopen("B.txt","w");assert(w!=NULL);//读写文件&nbsp;stu a[128];int i=0;while(fscanf(r,"%s%d%s%f",a[i].name,&a[i].no,a[i].sex,&a[i].score)!=EOF){printf("%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//输出到显示器屏幕&nbsp;fprintf(w,"%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//输出到文件B.txt&nbsp;i++;}&nbsp;&nbsp;//关闭文件&nbsp;fclose(r);fclose(w);system("pause");return 0;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP