阅读从文本文件中字符串和整数 [英] Reading both string and integer from a text file

查看:174
本文介绍了阅读从文本文件中字符串和整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我有一个文件看起来像这样

Let say I have a file looks like this

51.41 52.07 52.01 51.22 50.44 49.97 Coal Diggers
77.26 78.33 78.29 78.12 77.09 75.74 Airplane Flyers
31.25 31.44 31.43 31.09 31.01 30.92 Oil Fracting and Pumping
52.03 12.02 12.04 22.00 31.98 61.97 Big Bank
44.21 44.32 44.29 43.98 43.82 43.71 Rail Container Shipping
93.21 93.11 93.02 93.31 92.98 92.89 Gold Bugs

我想读通过的fscanf把浮法阵列和字号码字符串数组这个文件字。但是,费劲心思的几个小时后,我仍然无法弄清楚如何解决这件事情。

I want to read this file word using fscanf to put the numbers in float arrays and words in an array of strings. But, after few hours of strenuous thinking, I still can't figure out how to resolve this thing.

void dataInsert (COMPANY* company1, COMPANY* company2, COMPANY* company3, COMPANY* company4, COMPANY* company5, COMPANY* company6)
{
//Function Declaration
FILE* spData;
float number;
char* name[20];

//Statement
if ((spData = fopen("dataFile","r")) == NULL)
{
    fprintf(stderr, "ERROR OPENING!!");
    exit (1);
}

int i = 0;
int numCount = 0;
int lineCount = 0;
while (fscanf(spData, "%f", &number) != EOF)
{
    if(isdigit(number))
    {
        if (lineCount == 0)
        {
            company1 -> stock_price[i] = number;
        }
        else if (lineCount == 1)
        {
            company2 -> stock_price[i] = number;
        }
        else if (lineCount == 2)
        {
            company3 -> stock_price[i] = number;
        }
        else if (lineCount == 3)
        {
            company4 -> stock_price[i] = number;
        }
        else if (lineCount == 4)
        {
            company5 -> stock_price[i] = number;
        }
        else if (lineCount == 5)
        {
            company6 -> stock_price[i] = number;
        }

        numCount++;
        i++;
        if (numCount == 6)
        {
            lineCount++;
            numCount = 0;
            i = 0;
        }
    }
}//while
fclose (spData);
}//dataInsert

我不知道在每一行的末尾与琴弦做。我希望把公司结构的字符串 - >名称[10]。这些数据是在文本文件中。

I don't know what to do with strings at the end of each line. I want to put those string in structure company -> name[10]. Those data are in a text file.

推荐答案

如果该文件是正是格式,你可以使用 scanf()的容易。下面是一些code,让你开始;我没有测试过这一点,你需要填写一些缺失的东西。

If the file is in exactly that format, you can use scanf() easily. Here's some code to get you started; I haven't tested this and you need to fill in a few missing things.

#include <ctypes.h>  // for isspace()
#include <stdio.h> // for scanf(), getchar(), and EOF

char c2d[MAX_LINES][MAX_LENGTH_STRING_PER_LINE];
char *pstr;
float f2d[MAX_LINES][6]; // 6 floats per line
float *p;
int c, current_line_number;
char ch;
FILE *input;

input = fopen(...);
if (!input)
    ... handle the error

for (current_line_number = 0; ; ++current_line_number)
{
    // handle each line of input

    // first read 6 float values
    p = f2d + current_line_number;
    c = fscanf(input, "%f %f %f %f %f %f", p + 0, p + 1, p + 2, p + 3, p + 4, p + 5);
    if (c != 6)
        ... handle the error here

    // next grab string; stop at '<' or end of line or EOF
    pstr = c2d + current_line_number;
    for (;;)
    {
        ch = fgetc(input);
        if (ch == EOF || ch == '<' || ch == '\n')
        {
            *pstr = '\0';
            break;
        }
        *pstr++ = ch;
    }
    if (ch == '<')
    {
        // char was '<' so throw away rest of input line until end of line
        for (;;)
        {
            if (ch == EOF || ch == '\n')
                break;
            ch = fgetc(input);
        }
    }
    for (;;)
    {
        // eat up any white space, including blank lines in input file
        if (ch == EOF || !isspace(ch))
            break;
        ch = fgetc(input);
    }
    // once we have hit end of file we are done; break out of loop
    if (ch == EOF)
        break;
}

fclose(input);

我没有使用 scanf()的来阅读在该行的结束的字符串,因为它停止,当它击中空格,你的字符串值有空间它们。

I didn't use scanf() to read the string at the end of the line because it stops when it hits white space, and your string values have spaces in them.

如果输入文件是不是总是停在六浮点值,你就需要写code调用 scanf()的浮法在同一时间,直到你打一些不解析为float,你将需要做花车宽足以应付花车的数量最多,你会每行允许的数组。

If the input file isn't always six float values, you will need to write code to call scanf() one float at a time until you hit something that doesn't parse as a float, and you will need to make the array of floats wide enough to handle the largest number of floats you will permit per line.

祝你好运。

这篇关于阅读从文本文件中字符串和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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