CodeEval挑战:反转输入文件中的字符串 我决定在明年开始正式学习C++之前开始学习C++,我已经开始了一些关于科迪瓦和项目Euler的简单挑战。在本例中,您必须获取一个包含字符串的输入文件,并且您必须输出文件的行,并反转这些单词。这样,一个具有以下输入的文件

CodeEval挑战:反转输入文件中的字符串 我决定在明年开始正式学习C++之前开始学习C++,我已经开始了一些关于科迪瓦和项目Euler的简单挑战。在本例中,您必须获取一个包含字符串的输入文件,并且您必须输出文件的行,并反转这些单词。这样,一个具有以下输入的文件,c++,string,file,memory-management,C++,String,File,Memory Management,1:这是一号线 2:这是第二行 最终将成为 1:这是一行吗 2:这是两行吗 我编写了下面的程序来实现这一点,除了没有正确地反转字符串,而是完全反转单词之外,尽管编译时没有错误或警告,它仍然会出现分段错误。我想我已经错过了一些关于C++中正确内存管理的东西,但我不确定它是什么。那么,有人能告诉我在内存管理方面我遗漏了什么吗 #include <iostream> #include <fstream> #include <string> #include <

1:这是一号线

2:这是第二行

最终将成为

1:这是一行吗

2:这是两行吗

我编写了下面的程序来实现这一点,除了没有正确地反转字符串,而是完全反转单词之外,尽管编译时没有错误或警告,它仍然会出现分段错误。我想我已经错过了一些关于C++中正确内存管理的东西,但我不确定它是什么。那么,有人能告诉我在内存管理方面我遗漏了什么吗

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
int main(int argc, char** argv)
{
  std::string filename = argv[1]; //has to be argv[1], argv[0] is program name
  std::string output_string; //final output
  std::string line; //Current line of file
  std::ifstream read(filename.c_str());
  if(read.is_open()){
    while(std::getline(read,line)){
      std::string temp;
      std::istringstream iss;
      iss.str(line);
      while(iss >> temp){ //iterates over every word
        output_string.insert(0,temp); //insert at the start to reverse
        output_string.insert(0," "); //insert spaces between new words
      }
      output_string.erase(0,1); //Removes the space at the beginning
      output_string.insert(0,"\n"); //Next line
    }
    output_string.erase(0,1); //Remove final unnecessary \n character
    read.close();
  }
  else{
    std::cout<<"Unable to open file\n";
  }
  for(unsigned int i = output_string.length(); i>=0;i--){
    std::cout<<output_string[i];
  }
  std::cout<<"\n";
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
std::string filename=argv[1];//必须是argv[1],argv[0]是程序名
std::string output_string;//最终输出
std::string line;//文件的当前行
std::ifstream read(filename.c_str());
如果(读。是否打开()){
while(std::getline(read,line)){
std::字符串温度;
std::istringstream iss;
国际空间站str(直线);
而(iss>>temp){//迭代每个单词
输出_string.insert(0,temp);//在开始处插入以反转
输出_string.insert(0,“”;//在生词之间插入空格
}
输出_string.erase(0,1);//删除开头的空格
输出字符串。插入(0,“\n”);//下一行
}
输出_string.erase(0,1);//删除最后一个不必要的字符\n
read.close();
}
否则{

std::cout将最后一个for语句更改为

std::cout << output_string;
你能行

output_string.insert(start_of_line, temp); //insert at the start to reverse
同样正如长颈鹿船长所说,您正在反转整个文件,而不仅仅是每一行。因此,您可以反转每一行,并在完成该行后将其输出,而不是将整个输出存储到以后

以下是整个程序,只需进行少量更改,即可避免任何警告并获得正确的输出。主要更改是将
output\u string
的所有用法移到读取循环中

int main(int argc, char** argv)
{
    if (argc != 2)
    {
            std::cerr << "Need a file to process!" << std::endl;
            return 1;
    }
    std::string filename = argv[1]; //has to be argv[1], argv[0] is program name
    std::string line; //Current line of file
    std::ifstream read(filename.c_str());
    if(read.is_open()){
            while(std::getline(read,line)){
                    std::string output_string; //final output
                    std::string temp;
                    std::istringstream iss;
                    iss.str(line);
                    while(iss >> temp){ //iterates over every word
                            output_string.insert(0,temp); //insert at the start to reverse
                            output_string.insert(0," "); //insert spaces between new words
                    }
                    output_string.erase(0,1); //Removes the space at the beginning
                    std::cout << output_string << std::endl;
            }
            read.close();
    }
    else{
            std::cout<<"Unable to open file\n";
    }
}
int main(int argc,char**argv)
{
如果(argc!=2)
{
std::cerr temp){//迭代每个单词
输出_string.insert(0,temp);//在开始处插入以反转
输出_string.insert(0,“”;//在生词之间插入空格
}
输出_string.erase(0,1);//删除开头的空格

std::cout-Hmm,这确实修复了segfault,但如果我索引超出范围,为什么它会打印任何内容?我尝试时它没有打印任何内容。可能你的时钟设置为周一,segfault处理程序有点慢。奇怪,在我的上,它执行字符串反转,打印它,然后最后一行是segfault。嘿,谢谢你的帮助p、 另一方面,我一直在使用-Werror进行编译,但您是否建议将-Wextra和-Wall作为编译标准?@Thallazar警告越多越好。
-Wextra
添加了一些
-Wall
不包括的额外警告。
-Werror
转换通常“只是”的东西将警告转化为实际停止编译的错误,尤其是在学习时,这可能是一个好主意。也许
-Wall-Wextra-Werror
可能是一个好主意。
for(unsigned int i = output_string.length(); i>=0;i--){
   std::cout<<output_string[i];
}
reverse.cpp:31:49: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
   for(unsigned int i = output_string.length(); i>=0;i--){
int main(int argc, char** argv)
{
    if (argc != 2)
    {
            std::cerr << "Need a file to process!" << std::endl;
            return 1;
    }
    std::string filename = argv[1]; //has to be argv[1], argv[0] is program name
    std::string line; //Current line of file
    std::ifstream read(filename.c_str());
    if(read.is_open()){
            while(std::getline(read,line)){
                    std::string output_string; //final output
                    std::string temp;
                    std::istringstream iss;
                    iss.str(line);
                    while(iss >> temp){ //iterates over every word
                            output_string.insert(0,temp); //insert at the start to reverse
                            output_string.insert(0," "); //insert spaces between new words
                    }
                    output_string.erase(0,1); //Removes the space at the beginning
                    std::cout << output_string << std::endl;
            }
            read.close();
    }
    else{
            std::cout<<"Unable to open file\n";
    }
}