c语言从文件读取参数并修改

具体:从下面的文件内容中读取NUMBER-OF-NODES 后的值,并且判断如果这个值小于100则加5后替换掉原来的值,直到该值大于100,麻烦大神了
SIMULATION-TIME 40s
# The following is a random number seed used to initialize part of the seed of
# various randomly generated numbers in the simulation. This can be used to vary
# the seed of the simulation to see the consistency of the results of the
# simulation.
SEED 123
# The following two parameters stand for the physical terrain in which the nodes
# are being simulated. For example, the following represents an area of size 100
# meters by 100 meters. All range parameters are in terms of meters.
# Terrain Area we are simulating.
TERRAIN-DIMENSIONS (150, 150)
# The following parameter represents the number of nodes being simulated.

NUMBER-OF-NODES 50

# Terrain Area we are simulating.
TERRAIN-DIMENSIONS (150, 150)

经测试可用 

文件名定义为test.txt 修改后写入原文件

如需要可修改

#define FILE_NAME "test.txt"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "test.txt"

int main()
{
FILE *fp;
char *buf1, *buf2;
char *start, *p;
int size;
int remain_size;
int value;

fp = fopen(FILE_NAME, "rb");
if(fp == NULL)
{
printf("can not find file "FILE_NAME"\n");
return -1;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp) + 100;
fseek(fp, 0, SEEK_SET);
buf1 = malloc(size);
memset(buf1, 0, size);
fread(buf1, 1, size, fp);
fclose(fp);
start = strstr(buf1, "NUMBER-OF-NODES");
if(start == NULL)
{
printf("can not find pattern \"NUMBER-OF-NODES\" in file\n");
free(buf1);
return -2;
}
buf2 = malloc(size);
memset(buf2, 0, size);
start += strlen("NUMBER-OF-NODES");
p = start;
while(*p++ != '\n');
remain_size = size - 100 - (p-buf1);
memcpy(buf2, p, remain_size);
sscanf(start, "%d", &value);
printf("get NUMBER-OF-NODES value = %d\n", value);
while(value < 100) value+=5;
sprintf(start, "    %d\n", value);
memcpy(&start[strlen(start)], buf2, remain_size);
fp = fopen(FILE_NAME, "wb");
fputs(buf1, fp);
fclose(fp);
free(buf1);
free(buf2);
return 0;
}追问

有问题啊

追答

什么问题? 我这边用你发的测试文件是好的
test.txt原始用你发的
执行后
MULATION-TIME 40s
# The following is a random number seed used to initialize part of the seed of
# various randomly generated numbers in the simulation. This can be used to vary
# the seed of the simulation to see the consistency of the results of the
# simulation.
SEED 123
# The following two parameters stand for the physical terrain in which the nodes
# are being simulated. For example, the following represents an area of size 100
# meters by 100 meters. All range parameters are in terms of meters.
# Terrain Area we are simulating.
TERRAIN-DIMENSIONS (150, 150)
# The following parameter represents the number of nodes being simulated.

NUMBER-OF-NODES 100

# Terrain Area we are simulating.
TERRAIN-DIMENSIONS (150, 150)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-02-20
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp,*fp1;
char ch,str[99]="";
int flag=0,n;
fp=fopen("test.txt","r");
fp1=fopen("new.txt","w");
if(fp==NULL)return 1;
while(1)
{
fscanf(fp,"%s",str);
if(flag==1)
{
sscanf(str,"%d",&n);
while(n<100){n+=5;}
sprintf(str,"%d",n);
flag=0;
}
fprintf(fp1,"%s",str);
if(strcmp(str,"NUMBER-OF-NODES")==0) flag=1;
ch=fgetc(fp);
if(ch==EOF)break;
fputc(ch,fp1);
}
fclose(fp);
fclose(fp1);
return 0;
}

追问

有问题啊,请问这是怎么回事啊?

追答

test.txt和源文件同一目录

我和楼下的都是正确的,看来是你编译器有问题

追问

test.txt和源文件不是同一目录,我写全test.txt的路径不行么?

追答

可以,别写错就行,记得路径分隔符是\\

追问

马上采纳您的,你楼上的感觉还是有问题,您的可以正常运行,但是我要在原来文件中修改,因为我要做一个完全自动化的东西,不能建立新文件,您能不能再修改一下,谢谢,如果你觉得麻烦,我在追加50分

追答#include<stdio.h>
#include<string.h>
int main()
{
    FILE *fp,*fp1;
    char ch,str[99]="";
    int flag=0,n;
    fp=fopen("test.txt","r");
    fp1=fopen("new.txt","w+");
    if(fp==NULL)return 1;
    while(1)
    {
        fscanf(fp,"%s",str);
        if(flag==1)
        {
            sscanf(str,"%d",&n);
            while(n<100){n+=5;}
            sprintf(str,"%d",n);
            flag=0;
        }
        fprintf(fp1,"%s",str);
        if(strcmp(str,"NUMBER-OF-NODES")==0) flag=1;  
        ch=fgetc(fp);      
        if(ch==EOF)break;
        fputc(ch,fp1); 
    }
    fclose(fp);
rewind(fp1);
fp=fopen("test.txt","w");  //再写回原文件就行了
ch=fgetc(fp1);
while(ch!=EOF)
{
fputc(ch,fp);
ch=fgetc(fp1);
}
fclose(fp);
fclose(fp1);
remove("new.txt");  //删除文件    
    return 0; 
}

本回答被提问者采纳
第2个回答  2015-07-15
//c语言从读取并修改,参考实例如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#define FM "%s %s %s"
#define FMT "%s %s %s\n"
#define DT t.no,t.name,t.other
struct
{ char no[20]; //学号
char name[20]; //姓名
char other[20]; //其它
}stu[50],t; //t为临时变量

void add()
{
FILE *fp;
fp=fopen("data.txt","a+"); //追加
printf("Input NO,Name,Other:\n");
scanf(FM,DT);
fprintf(fp,FMT,DT);
fclose(fp);
}

void show()
{
FILE *fp;
fp=fopen("data.txt","r");
if(fp==NULL)exit(1); //异常处理
while(1)
{
fscanf(fp,FM,DT);
if(fgetc(fp)==EOF)break; //遇文件结束符
printf(FMT,DT);
}
fclose(fp);
}
void find()
{
FILE *fp;
char str[20];
int flag=0; //记录是否存在要查找的学生
fp=fopen("data.txt","r");
if(fp==NULL)exit(1);
printf("Input NO:");
scanf("%s",str); //输入要查找的学号
while(1)
{
fscanf(fp,FM,DT);
if(fgetc(fp)==EOF)
{
if(flag==0) printf("no this student!\n"); //文件读取结束还没找到该学生
break;
}
if(strcmp(str,t.no)==0) //学号匹配
{
printf(FMT,DT); //输出
flag=1;
}
}
fclose(fp);
}
void del(int n)
{
FILE *fp;
char str[20],name1[20],other1[20];
int i;
fp=fopen("data.txt","r");
if(fp==NULL)exit(1);
printf("Input NO:");
scanf("%s",str); //输入学号
for(i=0;i<50;i++) fscanf(fp,FMT,stu[i].no,stu[i].name,stu[i].other); //读取全部信息
fclose(fp);
fp=fopen("data.txt","w");
for(i=0;i<50;i++)
{
if(strcmp(str,stu[i].no)!=0) //学号匹配
fprintf(fp,FMT,stu[i].no,stu[i].name,stu[i].other); //原样写回
else
{
printf(FMT,stu[i].no,stu[i].name,stu[i].other); //输出修改或删除的信息
if(n==0) //修改
{
printf("Input the Name:");scanf(" %s",name1); //修改名字
printf("Input other:");scanf(" %s",other1); //修改其它
fprintf(fp,FMT,str,name1,other1); //写入修改信息
}
}
}
fclose(fp);
}
void menu()
{
char ch;
printf("\n=====================\n");
printf("== 1.Add 2.Show ==\n== 3.Del 4.Revise ==\n== 5.Find 0.Exit ==");
printf("\n=====================\n");
printf("Choice:");
ch=getche();printf("\n");//scanf(" %c",&ch);
switch(ch)
{
case '1':add();break; //添加
case '2':show();break; //显示
case '3':del(1);break; //删除
case '4':del(0);break; //修改
case '5':find();break; //查找
case '0':exit(0); //退出
default:printf("Input error!\n");break;
}
}

int main()
{
while(1){system("cls");menu();getchar();}//system("pause");
return 0;
}
第3个回答  2014-02-20
你下个Notepad++吧,能破译一下,