-1

这似乎应该很容易,这就是为什么它让我特别发疯。希望那里的人会立即看到问题。我只是想从根据用户输入构建的数组构建数组。它似乎创建了一个比我想要的更大的数组。这是程序:

    int main()
{

  ifstream inFile;
  ofstream outFile;

  int numReq, fileSize;
  string lang, dash;

  char fileName[40];
  char confirm[10];
  char confirm2[10];
  int character;
  char year[3];
  char month[1];
  char day[1];
  char hour[1];


  cout << "What file to process?" << endl;
  cin >> fileName;

  year[0] = fileName[14];
  year[1] = fileName[15];
  year[2] = fileName[16];
  year[3] = fileName[17];

  cout << "year = " << year << "." << endl;

  month[0] = fileName[18];
  month[1] = fileName[19];

  cout << "month = " << month << "." << endl;
  cout << "so I gotta know, what is..." << endl;
  cout << "month[0]? " << month[0] << endl;
  cout << "month[1]? " << month[1] << endl;
  cout << "month[2]? " << month[2] << endl;
  cout << "month[3]? " << month[3] << endl;
  cout << "month[4]? " << month[4] << endl;
  cout << "month[5]? " << month[5] << endl;
  cout << "month[6]? " << month[6] << endl;


  day[0] = fileName[20];
  day[1] = fileName[21];

      cout << "day = " << day << "." << endl;

  hour[0] = fileName[23];
  hour[1] = fileName[24];

  cout << "hour = " << hour << "." << endl;

  cout << "so, 'fileName[23]' is = " << fileName[23] << "?" << endl;
  cin >> confirm;

  cout << "So, the year is " << year << ", the month is " << month
       << ", the day is " << day << ", the hour is " << hour << "?" << endl;
  cin >> confirm;

  //cout << "Is this what you chose? " << fileName << endl;                           
  //cin >> confirm;                                                                   
  //cout << "Which character to manipulate?" << endl;                                 
  //cin >> character;                                                                 

  //cout << "This one? " << fileName[character] << endl;                              
  //cin >> confirm2;                                                                  

  inFile.open(fileName);
  assert (!inFile.fail());


  outFile.open("revisedPracticeFile1.txt");

  outFile << fixed << showpoint; // I have no idea what this is...                    
  outFile << setprecision(2);    // .. or this for that matter.                       

  cout << "Processing data" << endl;

  inFile >> lang;

  while (!inFile.eof() ){
    if (lang.length() <= 2){

      outFile << lang << " ";


      // I should keep in mind, that, for whatever reason, it seemed like the         
      //item 'setw(6)' made the program work when I put it in, but didn't seem        
      //to make the program stop working when I took it out. Curious..                

      inFile >> dash >> numReq >> fileSize;

      outFile << numReq << " " << fileSize << endl;

    }
    else{
      inFile >> dash >> numReq >> fileSize;
          cout << "took out " << lang << " " << numReq << " " << fileSize << endl;
    }

    inFile >> lang;
  }
  inFile.close();
  //assert(!inFile.fail());                                                           
  outFile.close();

  return 0;
}

...而且,这就是我运行程序时发生的情况:

    What file to process?
projectcounts-20090101-010000                                      
year = 2009.
month = 01009.
so I gotta know, what is...
month[0]? 0
month[1]? 1
month[2]? 0
month[3]? 0
month[4]? 9
month[5]? 
month[6]? 
day = 011009.
hour = 0111009.
so, 'fileName[23]' is = 0?
yes
So, the year is 1009, the month is 11009, the day is 111009, the hour is 0111009?
^C

......所以什么给了?

4

1 回答 1

2

该语法char year[3];声明了一个包含 3 个元素的数组。但是,然后你用它来存储 4 个元素。您的其他阵列也存在类似问题。

此外,您将 char 数组用作字符串。这是一种 C(不是 C++)的做事方式。当然,如果您愿意,您可以这样做。但是,这些 c 风格的字符串使用最后一项为零的约定。

因此,如果你想要一个 C 风格的字符串来存储工作 'foo',你可以这样做

char string[10];  // anything bigger than 3 works
string[0] = 'f';
string[1] = 'o';
string[2] = 'o';
string[3] = '\0';  // this zero tells functions like `printf` that the string has ended.

如果没有最后一个零,类似的函数printf将继续输出内存位置,直到它在某处发生零。

编辑:考虑使用 c++ std::string 进行字符串处理。

于 2012-10-15T00:02:08.513 回答