读写csv文件

时间:2017-05-16 03:50:39

标签: c++ csv fstream

这个程序的功能就是这个。当用户运行它时,他们被要求输入3个。日期(DDMMYY)身体部位训练(例如胸部)和时间(例如100)取决于条件的用户输入。用户可以存储新数据。或查看存储的数据。

当用户存储数据时,数据会很好地传输到文件中。当他们查看这些数据时会显示它们。这是正确的预期功能。

当他们重新启动程序并输入另一组信息时,会出现问题。这组信息会覆盖最后一组。所以只显示最新的数据条目。

1。如何在程序重新启动并输入新数据时将数据附加到文件而不是覆盖它? 2.我的另一个问题是如何在程序的同一次运行中重复该功能? 一旦我按ctrl z结束输入文件就停止了 并按任意键继续......

public class MeteringlogBean implements Serializable {

private static final long serialVersionUID = 1L;
private Date date10;
private Date date11;
private String d;
private String d1;
private String s;
private String text;
private FileWriter fw;
private BufferedWriter bw;
private String Fn = "C:/Users/swetha.papireddy/Documents/new/filename.txt";

public Date getDate11() {
    return date11;
}

public void setDate11(Date date11) {
    this.date11 = date11;

}

private String environment;

public String getEnvironment() {

    return environment;
}

public void setEnvironment(String environment) {

    this.environment = environment;
}

public Date getDate10() {
    return date10;
}

public void setDate10(Date date10) {
    this.date10 = date10;
}

public void save() throws IOException, NullPointerException {

    try {
        File file = new File(getEnvironment() + "/ischange.log");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append("\n");
        }
        fileReader.close();

        System.out.println("Contents of file:");
        s = stringBuilder.toString();
        System.out.println(s);

    } catch (IOException e) {
        e.printStackTrace();
    }

    // System.out.println(date10);
    d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date10);
    System.out.println("date:-" + d);
    d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date11);
    System.out.println("date:-" + d1);
    fw = new FileWriter(Fn);
    bw = new BufferedWriter(fw);
    bw.write(s);
    bw.close();

    File file = new File(Fn);
    FileInputStream fis = new FileInputStream(file);
    byte[] bytes = new byte[(int) file.length()];
    fis.read(bytes);
    fis.close();
    String text = new String(bytes, "UTF-8");
    String str1 = new String(d);
    String str2 = new String(d1);
    System.out.println(text.substring(text.indexOf(str1),
            text.lastIndexOf(str2)));
}

workoutlogger.h

workoutlogger.cpp

#include "workoutlogger.h"
#include <iostream>
#include <fstream>
#include <string>
#include "mainclient.h"

using namespace std;


workoutlogger::workoutlogger()
{
    int choices;
    cout << " (1) Do you want to log a new workout\n (2) Track previous workouts?\n Enter the number of your choice\n";
    cin >> choices;

    switch (choices) {
    case 1:
        log();
        break;

    case 2:
        viewinfo();
        break;
    default:
        cout << "invalid option..";
        workoutlogger();
    }


}


workoutlogger::~workoutlogger()
{
}

int workoutlogger::log()
{
    ofstream theFile("workinfo.txt");

    cout << "enter date (DDMMYY), bodypart trained (eg. Chest), time trained (mins)" << endl;
    cout << "press ctrl + z to quit\n";

    int date;
    string bodypart;
    int minutes;

    while (cin >> date >> bodypart >> minutes) 
    {

        theFile << date << ' ' << bodypart << ' ' << minutes << endl;
    }

        system("pause");
        return 0;
}

int workoutlogger::viewinfo() {

        ifstream theFile("workinfo.txt");

        int date;
        string bodypart;
        int minutes;

        while (theFile >> date >> bodypart >> minutes) { //stores infomation in these variables
                                                         //file pointer starts at first piece of info, then onto next info to store in variables
            cout << date << ", " << bodypart << ", " << minutes << endl;

        }
        system("pause");
        return 0;
}

1 个答案:

答案 0 :(得分:0)

打开一个文件进行追加非常简单,只需像之前一样打开它,就像这样传递append标志

for item in estArrivalTime {
                 if let _staNm = item["staNm"] as? String {
                     print("staNm : \(_staNm)")
                 }

             }

要退出,为什么不直接查找“退出”输入并假设这是一个退出条件且无效输入。

类似的东西:

std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::app);