使用C从文本文件读取输入参数 [英] reading input parameters from a text file with C

查看:125
本文介绍了使用C从文本文件读取输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个通用函数,该函数将从格式化的文本文件中读取参数。我希望它足够灵活以使参数列表可以变化。在C中完成此操作的最佳方法是什么?

I am trying to write a general function that will read in parameters from formatted text file. I want it to be flexible enough that the parameter list can vary. What is the best way to accomplish this in C?

我为此苦苦挣扎了几天。我从文件中提取的字符串不是我期望的。我用来调试的示例文本文件很简单:

I've been struggling with this for a few days. The strings that I'm able to extract from the file are not what I expected. The sample text file I'm using to debug is simple:

Nx : 1600;
Ny : 400;
dx : .524584;
dy : .25;
dt : 1;

我的程序在下面。

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

int main(int argc, char * argv[])
{
if(argc!=2)
{   
    printf("ERROR:  Usage: ./Practice3 <input file>");      
}
else
    {
FILE * fr = fopen(argv[1], "rt");


if(fr == NULL){printf("file %s not found", argv[1]);}

char * tmpstr1 ;
char * tmpstr2 ;

char * delimPtr;
char * endPtr;

int Nx = 0;
int Ny = 0;
double dx = 0;
double dy = 0;

    char tempbuff[100];

    while(!feof(fr)) 
    {
         if (fgets(tempbuff,100,fr)) {

            delimPtr = strstr(tempbuff,":");
            endPtr = strstr(delimPtr,";");
            strncpy(tmpstr1,tempbuff,delimPtr-tempbuff);
            strncpy(tmpstr2,delimPtr+2 ,endPtr-delimPtr-2);
            printf("<<%s>>\n",  tmpstr1);
            printf("<<%s>>\n",  tmpstr2);

            if (strcmp(tmpstr1,"Nx")==0) {
                 Nx = atoi(tmpstr2);
            } 
            else if (strcmp(tmpstr1,"Ny")==0) {
                 Ny = atoi(tmpstr2);
            }
            else if (strcmp(tmpstr1,"dx")==0) {
                 dx = atof(tmpstr2);
            }
            else if (strcmp(tmpstr1,"dy")==0) {
                 dy = atof(tmpstr2);
            }
            else{
                printf("Unrecongized parameter : \"%s\"\n", tmpstr1);
            }


         }


    }


    fclose(fr);

    printf("\nNx : %d \nNy : %d  \ndx : %f  \ndy : %f \n",  Nx,Ny,dx,dy);


}//end of code executed when input is correct 


}

我正在使用gcc -std = c99进行编译。
tmpstr1变量的末尾输出带有奇怪的blob字符。我无法像现在一样可靠地提取参数名称。什么是更好的方法?

I am compiling with gcc -std=c99. The tmpstr1 variable prints out with a weird blob character at the end. I can't reliably extract the parameter name the way I have it now. What is a better way?

此外,tmpstr2似乎并没有被strncpy完全覆盖,因此数字混淆了。似乎C并非旨在轻松进行这种刺痛操作而设计的。但是我必须在课堂上使用C语言,所以我陷入了困境。有想法吗?

Also, it seems that tmpstr2 doesn't get overwritten completely from strncpy so the numbers are getting mixed up. It seems like C is not designed to do this kind of sting manipulation easily. But I have to use C for class so I'm stuck. Any ideas?

推荐答案

char tmpstr1[16];
char tmpstr2[16];
...
/*
delimPtr = strstr(tempbuff,":");
endPtr = strstr(delimPtr,";");
strncpy(tmpstr1,tempbuff,delimPtr-tempbuff-1);
strncpy(tmpstr2,delimPtr+2 ,endPtr-delimPtr-2);
*/
sscanf(tempbuff, "%15s : %15[^;];", tmpstr1, tmpstr2);

这篇关于使用C从文本文件读取输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆