【问题标题】:File Handling c++ in linuxlinux中的文件处理c++
【发布时间】:2021-03-21 16:23:56
【问题描述】:

您好,我正在尝试在 ubunto linux 中进行文件处理。我的代码在 Windows 的虚拟工作室中运行良好,但在 Linux 中出现问题。以下是错误的代码和屏幕截图。该代码是读取文件检查文件中是否有整数,如果有则将其增加 1。 代码:

#include<iostream>
#include<stream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
 ifstream fin;
 ofstream fout;
 char abc[20];
 fin.open("input.txt");
 fout.open("output.txt");
 while (!fin.eof())
 {
     fin.getline(abc, 20);
     for (int j = 0; abc[j] !=0; j++)
     {

         char ab[20];
         int s = abc[j];
         int i = 0;
         bool check = false;

         while ((s >= 48) && (s <= 57))
         {
             
             check = true;
             
             ab[i] = abc[j];
             i++;
             j++;
             s = abc[j];
             
         }
         if (check)
         {
             ab[i] = 0;
             i = 0;
             int a = atoi(ab);
             a++;
             fout << a;
         }
         else
             fout << abc[j];


     }
     fout << endl;
     for (int k = 0; k < 20; k++)
     {
         abc[k] = 0;
     }
 }
 fin.close();
 fout.close();


}

error given in linux

【问题讨论】:

  • gcc -> g++.
  • gcc 用于编译 C,g++ 用于 C++。

标签: c++ file file-handling


【解决方案1】:

gcc 用于编译 C 程序。你应该使用g++来编译C++程序。

【讨论】:

  • 否则你可以在 gcc 选项中添加 -lstdc++ 来编译 C++ 代码
  • @SannanYousuf 按左侧的绿色勾号将您的问题标记为已解决。
猜你喜欢
  • 2013-07-19
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
相关资源
最近更新 更多