使用vector为什么代码写出来就会出错(标签-C语言)

C语言读取文件的时候按照行读取,然后按照列写入文件,这个过程必须使用数组么?使用vector为什么代码写出来就会出错?是不是长度问题

不一定用数组,队列、链表都可以,vector报错长度也有一定关系,得具体分析才行

需要看一下代码

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:小练习 - C语言实现一个极简通用vector
  • 除此之外, 这篇博客: C/C++ 虚函数与多态(含vector用法) 及获取文件内容、打印字符串实例中的 4完整代码 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    注意头文件,这次实验包含的头文件很多

    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include <sstream>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    //测试文件:a.txt,可选择其他同一目录下的文件进行测试
    
    class StrHandler
    {
    public:
    	std::string str;
    	std::vector<string> v;
    public:
    	StrHandler() {};
    	virtual void Handle(const std::string &file) {};
    };
    
    class printStr
    	:public StrHandler
    {
    public:
    	printStr() {};
    	void Handle(const std::string &file)
    	{
    		str = file;
    		cout << str << endl;
    	}
    };
    
    class getLongStr
    	:public StrHandler
    {
    public:
    	getLongStr()
    		:StrHandler()
    	{};
    
    	void Handle(const std::string &file)
    	{
    		str = file;
    		string p = str;
    		string temp1, ans = "";
    		istringstream string_in(str);
    		while (string_in) {
    			string_in >> temp1;
    			if (temp1.length() > ans.length()) {
    				ans = temp1;
    			}
    		}
    		cout << "字符数最多的字符串是:" <<endl<< ans << endl;
    
    	}
    
    };
    
    class getLowerStr
    	:public StrHandler
    {
    public:
    	getLowerStr()
    		:StrHandler()
    	{};
    	string getLower(const std::string &file)
    	{
    		str = file;
    		transform(str.begin(), str.end(), str.begin(), ::tolower);
    		return str;
    	}
    	void setVector(const std::string &file)
    	{
    		string lowerfile;
    		string buf;
    		lowerfile = getLower(file);
    		stringstream ss(lowerfile);
    
    		while (ss >> buf)
    			this->v.push_back(buf);
    	}
    	void Handle(const std::string &file)
    	{
    		setVector(file);
    		cout << "全部转换为小写:"<<endl << str << endl;
    	}
    };
    
    
    string readFileIntoString(char *filename)
    {
    	ifstream ifile(filename);
    	ostringstream buf;
    	char ch;
    	while (buf&&ifile.get(ch))
    		buf.put(ch);
    	return buf.str();
    }
    
    void readStrings(const std::string &file,StrHandler *p)
    {
    	p->Handle(file);
    }
    
    
    int main()
    {
    	char filename[20];
    	string file;
    
    	cout << "请输入文件名:" << endl;
    	cin.getline(filename, 20);
    	file = readFileIntoString(filename);
    	
    	printStr h1;
    	getLongStr h2;
    	getLowerStr h3;
    
    	StrHandler *a = &h1;
    
    	readStrings(file, a);
    	a = &h2;
    	readStrings(file, a);
    	a = &h3;
    	readStrings(file, a);
    
    	getchar();
    	return 0;
    }
    
  • 以下回答来自chatgpt:

    在这个问题中,我们没有看到具体的报错信息和代码,所以很难确切地找到问题所在。vector的长度不一定是问题的根本原因。在这里,我可以提供以下几个建议:

    1.检查文件路径和读取的文件是否为空或存在问题。

    可以使用文件是否存在的函数来检查文件路径是否正确。例如,使用access()函数检查文件是否存在:

    #include<io.h>
    // ...
    //如果返回值不为 -1 表示文件存在
    if (_access_s(file_name, 0) == -1) {
        throw exception("文件不存在!");
    }
    

    如果文件为空或者读取文件时出现问题,可能会导致某些数据输入不正确。这可能是问题的根本原因之一。

    2.检查你的读取和写入数据的方法是否正确。

    可以确定你是否使用了正确的方式读取和写入数据。例如,使用ifstream和ofstream读写文件时需要确保以适当的方式读写数据。正确的读写文件方式如下:

    //正确的打开文件方式
    ifstream in("filename.txt"); //read
    ofstream out("filename.txt"); //write 
    if (!in) { //处理无法正常读取文件的情况
        throw exception("无法打开文件");
    }
    
    if (!out) { //处理无法正常写入文件的情况
        throw exception("无法写入文件");
    }
    

    3.向你的代码添加一些辅助代码。

    添加一些辅助代码可以帮助你更容易地找到你的代码中的问题。例如,你可以添加以下代码打印输出vector中的数据是否正确:

    for (int i=0;i<v.size();i++)
        cout<<v[i]<<endl; 
    

    4.使用基本的C写法来确保你的程序正确性。

    你可以不使用vector,仅使用基本的C写法来确保你的程序正确性。例如,使用二维字符数组或指针数组。这将能够帮助你确认问题是否起因于vector。

    最后,我建议你检查你的读取和写入文件的方式是否正确,以及检查文件路径和文件数据是否存在问题并添加一些辅助代码来检测你的代码的每个步骤是否正确执行。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^