在Visual C ++中传输文件到文件数据

时间:2011-12-08 13:53:03

标签: file visual-c++

我有一个代码:

    using namespace std;
    int main()
        {
              char Filename[100];
              char st[100][100];
              char *argv[100];
          std::ifstream infile("data.txt");
          std::freopen("output.xls","w",stdout);
    if (!infile)
       {
          cout << " Input file Could not open file." << endl;
          return 1;
       }
    std::string line;
    while (std::getline(infile,line))
       {   
            int a;
            int length=line.size();
            for(a=0;a<=length;a++)
              {
                     Filename[a]=line[a];
              }
       // one D array to 2D array
         int b=0;
         int l=0;
          strcpy(st[0],"0");// for erase previous value
          //cout<<st[0]<<endl;
      for(int j=0;j<=a;j++)
       {
           if (Filename[j]==' ')
             {
                    b++;
                    l=0;
                     strcpy(st[b],"0");// for erase previous value
              }
       else
        {
           st[b][l]=Filename[j];
           l++;
        }

      } //..........
    // 2D array to reffarence array.
      for(int k=0;k<=b;k++)
          {
              argv[k]=&st[k][0];
               cout<<argv[k];// or  cout<<st[k]; i get same 
               cout<<"\t";

           }
         cout<<endl;

           //..............
    // Do whatever with the array content
       }

        infile.close();


         return 0;
      }

数据文件是:(两个单词之间的一个空格)   testCore 100 100 400 5000 5000“/行结束”   testCo 1 4 4 100 100“/行尾”   tcore 1 3 4 5 20“/行结束”   核心2 3 4 200 4“/行结束”

我的输出是: enter image description here

我想要的:我想逐行读取数据文件(这很重要)并在读取一行后分隔每个单词,然后读取另一行。但问题是在每行的第一个单词的输出中(在第一行的每个单词中)它给出了垃圾输出。第2栏也有问题。我的代码有什么问题?有没有任何更正?

1 个答案:

答案 0 :(得分:0)

您正在创建一个大小为100的char数组,它未初始化。然后,你只需要阅读3-8个字符。但是你要打印出整个缓冲区,包括未初始化的元素。