简体   繁体   English

C从竖线字符分隔文件中读取

[英]C reading in from a vertical line character delimited file

new to C and trying to read in a text file, "stocks", delimited with vertical line '|' C的新手,并试图读取文本文件“ stocks”,以竖线“ |”分隔 characters. 字符。 Aim is to open the file which is has a string and float value on each line, in the format: 目的是打开文件,文件的每一行都有一个字符串和浮点值,格式为:

TOM|149.62
JIM|23.25

I have read other posts about commas, colon and tab delimited files, but the "scanset" suggestion applied here as a [^|] between the %s and %f doesn't seem to work. 我已经阅读了其他有关逗号,冒号和制表符分隔文件的文章,但是在这里将“ scanset”建议作为%s%f之间的[^|]应用似乎无效。 I seem to have managed to store the first character of the string at least now, but the float value saved is nonsense. 我似乎至少现在已经设法存储了字符串的第一个字符,但是保存的float值是胡说八道。 This will later be writing to the arrays declared after the file but the basic case of displaying the string and float value for each line means I can continue on my own. 这将稍后写入文件后声明的数组,但是显示每行的字符串和浮点值的基本情况意味着我可以自己继续。 Appreciate any help you can give me. 感谢您能给我的任何帮助。

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

// TOM|149.62
// JIM|23.25


int main()
{
    FILE *stocks;

    char *stock_Tickers[100];
    float stock_Prices[100];

    if ((stocks = fopen("Stocks.txt", "r")) == NULL)
    {
        fprintf(stderr, "Error opening file.\n");
        exit(1);
    }

    char tempchar;
    float tempfloat;

    for (int i = 0; i < sizeof (stock_Tickers); i++)
    {
        if (feof(stocks))
        {
            break;
        }
        fscanf(stocks, "%s[^|]%f\n",&tempchar,&tempfloat);
        cout << tempchar << " " << tempfloat;
        cin.get();
    }
    return 0;
}

Update: @Michel Billaud Apologies, but I have one last error here. 更新:@Michel Billaud抱歉,但我在这里有最后一个错误。 Your method worked perfectly, but when I try it on this slight variant it starts printing rubbish on the last floating point and breaks on subsequent loops. 您的方法效果很好,但是当我在这个微小的变体上尝试时,它开始在最后一个浮点上打印垃圾,并在随后的循环中中断。 Looked at the local variables and the last array just isn't updating. 看了局部变量,最后一个数组没有更新。 I think it has to do with the new line, as when I change the float for an integer it still doesn't work. 我认为这与新行有关,因为当我将浮点数更改为整数时,它仍然不起作用。 Can you see what I might be doing wrong as it seems the same format to me? 您能看出我可能做错了什么,因为对我来说格式似乎相同? Format this time is Ryan|B|IBM|100|176.10 . 这次的格式为Ryan|B|IBM|100|176.10 All files are read in after this...Thanks. 此后将读取所有文件...谢谢。

FILE *trades;
    // Ryan|B|IBM|100|176.10    
    // Ryan|S|IBM|50|177.10

    char trade_User[100][20];
    char trade_Type[100];
    char trade_Tickers[100][4];
    int trade_Quantity[100];
    float trade_Prices[100];
    int trade_Count = 0;

    if ((trades = fopen("Trades.txt","r")) == NULL)
    {
        fprintf(stderr, "Error opening file.\n");
        exit(1);
    }

    for (int i = 0; i < sizeof(trade_Tickers); i++)
    {
        if (feof(trades))
        {
            break;
        }
    fscanf(trades, "%19[^|]|%1[^|]|%4[^|]|%d[^|]|%f\n", 
        &trade_User[i], &trade_Type[i], &trade_Tickers[i], &trade_Quantity[i], &trade_Prices[i]);
    printf("%s %c %s %d %f\n", 
        trade_User[i], trade_Type[i], trade_Tickers[i], trade_Quantity[i], trade_Prices[i]);
    trade_Count++;
}

Hint: when trying to use a problematic feature, do it in a small separate program. 提示:尝试使用有问题的功能时,请在一个单独的小型程序中进行操作。 So you won't take the risk to draw wrong conclusions because of another unrelated issue. 因此,您不会冒险因为另一个不相关的问题而得出错误的结论。

// just playing with |-separated fields

#include <stdio.h>

int main(int argc, char **argv)
{
    char line[]="TOM|1234.56";
    char name[20];
    float value;
    sscanf(line, "%[^|]|%f", name,  &value);
    printf("name = %s, value = %f\n", name, value);
    return 0;
}

By the way: seems you're using the C++ compiler (because of "using namespace std"). 顺便说一句:似乎您正在使用C ++编译器(因为“使用命名空间标准”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM