CSV文件的读写

csv文件按照“,”进行分隔。

1.CSV文件的写入

#include <iostream>
#include <fstream>

int main()
{
    char filename[100];
    int num(1);
    sprintf(filename, "my_file_%d.csv", num);
    ofstream outfile(filename, ios::out);
	if (!outfile)
	{
		cerr << "open error!" << endl;
		return -1;
	}
	outfile.setf(ios::right);

    //写入数据
    outfile<<"title1"<<endl;
    outFile << "name" << ','
            << "income" << ','
            << "expenditure" << ','
            << "addr" << std::endl;
    // ********写入两行数据*********
    // 写入字符串(数字)
    outFile << "zhangsan" << ','
            << "3000" << ','
            << "1200" << ','
            << "中国 北京市" << std::endl;
    // 写入浮点数(转为字符串)
    outFile << "lisi" << ','
            << std::to_string(2032.1) << ','
            << std::to_string(789.2) << ','
            << "中国 陕西省" << std::endl;
    outfile.close();
    return 0;
}

2.CSV文件读取

#include <iostream>
#include <istream>
#include <streambuf>
#include <fstream>
#include <sstream>  //istringstream的头文件
#include <vector>
#include <string>
#include <stdlib.h>

int main()
{
    string fileName="test.csv";    
    ifstream infile;
    infile.open(fileName,ios::in);
    if (!infile.is_open())
    {
        cout << "打开文件"<<fileName <<"失败" << endl;
        infile.close();
        return;
    }
    else
    {
        string line;
        string word;
        //---------------------读取数据---------------------
        //读标题
        getline(inflie,line);
        istringstream sin;  //从string对象line中读取字符
        //按行读取数据
        while(getline(infile,line))
        {
            sin.clear(); //多次使用需要清空
            sin.str("");//清空stringstream的缓冲,每次循环内存消耗不增加
            sin.str(line);
            //将sin中的字符,以逗号为分隔,读到字符串数组words中
            while(getline(sin,word,','))
            {
                cout<<word<<end;
            }
        }
        infile.close();
        return 0;
    }
}
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    string source = "abc,123,<!>";
    stringstream ss(source);
    cout << ss.str() << endl;
    
	cout<< endl;

    string str;
    while (getline(ss, str, ','))
    {
        cout << str << endl;
    }

    return 0;
}

/*
输出:
abc,123,<!>

abc
123
<!>
*/

getline()

#include<string>
std::istream& getline(std::istream& is, std::string& str, char delim);
/*
is:输入流对象,可以是 std::cin(标准输入)、文件流对象(std::ifstream)、字符串流对象(std::istringstream)等等。
str:存储读取到的一行文本的字符串。
delim:分隔符,表示在哪个字符处停止读取,默认为换行符 '\n'。
*/
//返回:输入流对象的引用,通常是 is,表示函数成功执行。

在每次调用 getline() 函数后,输入流的指针会自动移动到下一行的开头(如果是默认的分割符'\n'),准备读取下一行文本。

1.从标准输入中读取一行:

#include <iostream>
#include <string>
 
int main() 
{
    std::string line;
    std::cout << "Enter a line of text: ";
    std::getline(std::cin, line);
    std::cout << "You entered: " << line << std::endl;
    return 0;
}
/*
输入字符串:asf
输出字符串:asf
*/

2.从文件中读取一行

#include <iostream>
#include <fstream>
#include <string>
 
int main() {
    std::ifstream file("example.txt");
    std::string line;
    if (file.is_open()) 
    {
        while (std::getline(file, line))
        {
            std::cout << line << std::endl;
        }
        file.close();
    } 
    else 
    {
        std::cout << "Unable to open file." << std::endl;
    }
    return 0;
}

3.从字符串流中读取一行

#include <iostream>
#include <sstream>
#include <string>
 
int main() 
{
    std::istringstream ss("Hello World!\nThis is a new line.");
    std::string line;
    while (std::getline(ss, line)) 
    {
        std::cout << line << std::endl;
    }
    return 0;
}

形式2举例:

#include<iostream>
#include<bits/stdc++.h>
#include<string>
using namespace std;
int main(){
	string s;
	cout<<"输入字符串:"; 
	getline(cin,s,'c');
	cout<<"输出字符串:"; 
	cout<<s;
	return 0;
}
/*
输入:sdfhsjfhcjdhf
输出:sdfhsjf
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值