通过读取excel表为c文件中的变量赋值

Assign values to variables in c file by reading excel sheet

本文关键字:变量 赋值 文件 读取 excel 表为      更新时间:2023-10-16

我需要通过读取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
    • 在源文件中搜索某个内容并写入输出文件

当您从输入CSV文件中删除每一新行的输出文件时,您不能得到超过上一次的操作。

如果在循环之外打开一次输出文件,会简单得多。

而且。。。CCD_ 5是反模式。您可以查看在尝试读取一行之前是否已到达文件的末尾,然后在第一个getline设置了eof标志时读取4个值。你必须在阅读后立即测试eof,而不是在。。。