通过读取excel表为c文件中的变量赋值
Assign values to variables in c file by reading excel sheet
我需要通过读取excel表为C文件中的变量赋值。我已经写了一段代码,但自从我使用for循环以来,只有最后一个变量被赋值。它覆盖了分配给以前变量的值,因为我在分配值后创建了一个不同的输出文件。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s1, s2, s3,s4;
string filename, text, line;
string cfilename,funcname, signal, value;
int i , k , m;
cout << "Enter excel filename" << endl;
cin >> filename;
cout << "How many lines of text are in the file?" << endl;
cin >> m;
fstream file(filename);
if (!file) {
cerr << "No such file exists." << endl;
exit(1);
}
if (file.is_open()) {
while (file.eof()==0){
for (k = 0; k < m; k++) { //Loops for as many lines as there are in the file
for (i = 0; i < 4; i++) { //Loops for each comma-separated word in the line
if (i == 0){
getline(file, text, ',');
cfilename=text;
cout << cfilename << 't';}
else if (i == 1){
getline(file, text, ',');
funcname=text;
cout << funcname << 't';}
else if (i == 2){
getline(file, text, ',');
signal=text;
cout << signal << 't';}
else if (i == 3){
getline(file, text, 'n');
value=text;
cout << value << 'n';}
}
string s1=signal,s2=value;
s2 = s2 + "; //";
int offset, inset;
string line;
string search=s1;
fstream cfile(cfilename);
fstream fileOutput;
fileOutput.open("output.c");
if(cfile.is_open() && fileOutput.is_open()) {
while(!cfile.eof()) {
getline(cfile, line);
if ((offset = line.find(funcname)) != string::npos){
cout << "found: " << funcname << endl;
string line1;
fileOutput << line << 'n';
skip:
while(getline(cfile,line1)){
if((inset=line1.find(search, 0)) !=string::npos){
cout<<"found: " << search << endl;
string s3 = s1+ "=" +s2;
//cout<<s3;
line1.replace( inset, inset+s1.size(), s3 );}
fileOutput << line1 << 'n';
goto skip;
}
getchar(); }
fileOutput << line << 'n'; }
cfile.close();
fileOutput.close();
}
}
}
}
file.close();
getchar();
return 0;
}
我试图先搜索一个函数,然后搜索该函数中的变量。
这里需要帮助。
我不确定这是问题所在,但。。。我认为while ()
太多了。
当您读取了文件的m
行时,您的
while (file.eof()==0)
结果为true,因为您在文件末尾之后什么都没读。
因此,您读取了其他m
行(失败,但无法控制读取的成功)。
编辑:我认为你应该写一些类似的东西
cout << "Enter excel filename" << endl;
cin >> filename;
fstream file(filename);
if (!file) {
cerr << "No such file exists." << endl;
exit(1);
}
while ( getline(file, cfilename, ',') && getline(file, funcname, ',')
&& getline(file, signal, ',') && getline(file, value, 'n')) {
cout << cfilename << 't' << funcname << 't' << signal << 't'
<< value << 'n';
string s1=signal,s2=value;
[...]
程序完全按照您的要求执行:
- 您逐行读取CSV文件(excel格式为二进制!)
- 对于csv中的每一行:
- 您擦除输出文件,因为您打开的fstream没有指定
ios::ate
- 在源文件中搜索某个内容并写入输出文件
- 您擦除输出文件,因为您打开的fstream没有指定
当您从输入CSV文件中删除每一新行的输出文件时,您不能得到超过上一次的操作。
如果在循环之外打开一次输出文件,会简单得多。
而且。。。CCD_ 5是反模式。您可以查看在尝试读取一行之前是否已到达文件的末尾,然后在第一个getline设置了eof标志时读取4个值。你必须在阅读后立即测试eof,而不是在。。。
相关文章:
- 为"adjacent"变量赋值时出现问题
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 如何在不使用赋值运算符的情况下为动态变量赋值?
- 为什么我不能为变量赋值函数?
- 可视化的c++变量赋值使代码正常工作
- 如何为结构类型变量赋值
- 使用 boost python 从 c++ 为 python 中的类成员变量赋值
- QT变量赋值,<没有这样的值>,SIGSEGV
- C++:为全局类变量赋值
- 从类外部为类中的变量赋值
- 在 c++ 中为字符串变量赋值
- 为类中的私有静态变量赋值
- 使用类为变量赋值 (C++)
- 在编译时为变量赋值
- C ++变量赋值,这是正常的方式吗
- 在C++中为类变量赋值后声明类变量
- 了解工厂方法和静态变量赋值的返回值优化 (Visual Studio)
- 如何在 c++ 中从函数获取返回值而不为其变量赋值
- 简单的引用变量赋值导致对象的全局指针出现segfault