【问题标题】:Assign values to variables in c file by reading excel sheet通过读取excel表为c文件中的变量赋值
【发布时间】:2016-03-22 09:05:30
【问题描述】:

我需要通过读取 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;
}

我正在尝试先搜索一个函数,然后再搜索该函数内的变量。

在这里需要一些帮助。

【问题讨论】:

标签: c++ excel file-handling


【解决方案1】:

我不确定是不是这个问题,但是...我觉得while () 太多了。

当您阅读文件的m 行时,您的

while (file.eof()==0)

结果为真,因为您在文件末尾没有读取任何内容。

因此,您阅读了其他 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 中为每一行编写整个输出文件吗?我只想从替换字符串的地方继续。
  • 我不确定我是否理解。您要做的只是 eleborare csv 文件的第一行?
  • 没有。我的 csv 文件在每一行中包含不同的变量及其对应的值。我想搜索变量及其值并将其写入输出文件,以便输出文件中的变量具有值。但是我上面的代码覆盖了 csv 文件中 1 行的整个输出文件,即只有 1 个变量被赋值。但我希望将所有值分配给变量。请帮助
【解决方案2】:

程序完全按照您的要求执行:

  • 你逐行读取一个CSV文件(excel格式是二进制!)
  • 对于 csv 中的每一行:
    • 擦除输出文件,因为您打开 fstream 时未指定 ios::ate
    • 在源文件中搜索内容并写入输出文件。

当您从输入 CSV 文件中删除每一行的输出文件时,您只能获得最后一个操作。

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

而且...while (file.eof() == 0) 是一种反模式。您在尝试读取一行之前查看是否已到达文件末尾,然后在第一个 getline 设置了 eof 标志时读取 4 个值。您必须在读取后立即测试 eof,而不是之前...

【讨论】:

  • 是的,这就是我面临的问题。你能告诉我如何避免在上面的代码中为 csv 中的每一行重写整个文件。另外,我尝试在上面的代码中使用 ios::ate,但不工作,我该怎么做?
  • 当我尝试使用 fstream 创建输出文件时,它不会创建和写入文件。我必须使用 ofstream 创建文件,该文件创建输出文件但不写入任何内容,然后在下次执行时将命令更改为 fstream 以写入文件。有什么解决办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多