分段错误不会出现在Visual Studio中,但会出现在学校服务器中

时间:2017-10-08 16:13:44

标签: c++ visual-studio

早上好,

我正在为学校编写一个简单的程序,该程序从.txt文件中读取行,并将每个字符的编号输出到新文件中。我已经被困了几个小时,因为我无法弄清楚分段错误发生的位置。当我在visual studio中编译和调试时,它不会发生,但是当我在学校的服务器上编译并运行我的程序时,它就会出现。我知道除了分段错误之外,程序中仍然存在缺陷,但我更喜欢自己完成这些工作:)。在此先感谢您的帮助。

主:

#include <iostream>
#include <fstream>
#include <string>
#include "letterFunctions.hpp"

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::iostream;
using std::ifstream;
using std::ofstream;
using std::ios;

void count_letters(ifstream &ifs, int* freqArray);

int main()
{
     //Declare the string for the input file and create the input stream.
     ifstream ifs;
     string inputFileName;

     //Initialize the array for the frequency of each letter.
     int *freqArray = new int[26];

     //Prompt the user for the input file name.
     cout << "Enter the name of the file to be analyzed." << endl;
     cin >> inputFileName;

     //Open the file.
     ifs.open(inputFileName.c_str());

     //If the file doesn't exist, prompt the user for a new file.
     while (ifs.fail())
     {
          cout << "Invalid entry. Enter the name of the file to be analyzed." << endl;
          cin >> inputFileName;

          ifs.open(inputFileName.c_str());
     }


    count_letters(ifs, freqArray);

     //Close the input file.
     ifs.close();


     return 0;
}

letterFunctions.cpp:

#include <fstream>
#include <iostream>
#include <string>
#include "letterFunctions.hpp"

using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;

ofstream ofs;

void count_letters(ifstream &ifs, int* freqArray)
{
     //Initialize new variables.
     int arrayModifier = 0;
     char input;

     //Read the first character of the file.
     input = ifs.get();
     while (input != EOF)
     {
          //Reset the array to 0's.
          for (int i = 0; i < 26; i++)
          {
               freqArray[i] = 0;
          }

          while (input != '\n')
          {
               //Convert all chars to upper case.
               if ((int)input >= 97 && (int)input <= 122)
               {
                    putchar(toupper(input));
               }

               //Set the array modifier to the corresponding letter, and add one to the counter.
               arrayModifier = ((int)input - 65);
               freqArray[arrayModifier]++;

               //Get the next character.
               input = ifs.get();
          }

          //Output to the file.
          output_letters(ofs, freqArray);

          input = ifs.get();
     }
}

void output_letters(ofstream &ofs, int* freqArray)
{
     string outputFileName;
     cout << "Enter the name of the file you would like to output this paragraph to." << endl;
     cin >> outputFileName;

     ofs.open(outputFileName.c_str());
     int l = 65;
     for (int i = 0; i < 26; i++)
     {
          ofs << (char)l << ": " << freqArray[i] << "\n";
          l++;
     }
     ofs.close();
}

1 个答案:

答案 0 :(得分:0)

您有一些未定义的行为,可以在您的电脑上正常运行,但不能在服务器上运行。

如果我给你以下文字"this is an error!\n"(忽略&#34;),请考虑会发生什么。