从.csv文件中的字段读取值? [英] Reading Values from fields in a .csv file?

查看:671
本文介绍了从.csv文件中的字段读取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我做了一个小的脚本,一些帮助阅读.csv文件。我虽然找到了一种方式来读取第一个值并存储它,但由于某种原因,它存储最后一个值。

Yesterday I made a small script with some help to read .csv files. I though I found a way to read the first value and store it, but for some reason it stores the last value instead.

我存储我认为应该是第一个值在value1下,并重新显示它,以确保它正确显示,并且实际上存储在可调用的变量下。

I store what I thought should be the first value under value1, and re-display it to make sure it displays properly and is in fact being stored under a callable variable.

有人知道这个代码有什么问题?我想我应该使用向量,但正如我读的参考表,我在互联网上发现他们,我是一个小抛出。任何帮助是赞赏。

Does anyone know what is wrong with this code? I think I should be using vectors but as I read the reference sheets I find on the internet about them I am being a little thrown of. Any help is appreciated.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{

    int loop = 1;
    string value;
    string value1;

    while(loop = 1)
    {

        cout << "Welcome! \n" << endl;

        ifstream myfile;

        myfile.open ("C:/Documents and Settings/RHatfield/My Documents/C++/Product Catalog Creator/Source/External/Sample.csv");

        while (myfile.good())

        getline ( myfile, value, ',' );
        cout << string ( value) << endl; 

        value1 = value;

        cout << value1;

        myfile.close();

        system("PAUSE");

        return 0;
    }


}


推荐答案

您的错误似乎是纯代码格式化。

Your error seems to be in pure code formatting.

while (myfile.good())

循环后没有 {} 因此只有下一行重复。

There is no {} after the loop. So only next line is repeated.

下面的代码是在读取整个文件后执行的。

The following code is executed after reading of the whole file.

cout << string ( value) << endl;

因此 value 文件。

这篇关于从.csv文件中的字段读取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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