【问题标题】:Inserting dot after 2 chars till the end of file in C++在 2 个字符后插入点直到 C++ 中的文件末尾
【发布时间】:2013-08-27 16:13:07
【问题描述】:

大家好

我想在读取指定数量的字符后插入一个点(或任何其他字符)(在我的情况下是 2)

这是我的代码:

#include <fstream>
#include <string>

using namespace std;

string dot = ".";        //Char to insert
char ch;
unsigned i=0;          //Symbol counter
int counter = 2;       //How much letters to skip before insertion


int main(){

fstream fin("file.txt", fstream::in);

while (fin >> noskipws >> ch) {

  ofstream file;
  file.open ("file2.txt");
  file << ch;
  file.close();
  i++;
       if(i == counter){
       file.open ("file2.txt");
           file << dot;
       file.close();
       i = 0;
       }
    }
 return 0;
}

我在新的 file2.txt 中写的是“0”。

附:我是 C++ 新手,所以请深入解释一下新手(如果你有时间)

提前谢谢你。

编辑:应用一些修复后,输出现在是“。”

EDIT2:它不允许我回答我自己的帖子(因为我是这个论坛的新手,必须等待 7 小时才能回答),我将在这里发布我的固定代码

固定版本:

#include <fstream>
#include <string>

using namespace std;

string dot = ".";        //Char to insert
char ch;
unsigned i = 0;          //Symbol counter
int counter = 2;         //How much letters to skip before insertion


int main(){

ofstream file;
file.open ("file2.txt");
fstream fin("file.txt", fstream::in);

while (fin >> noskipws >> ch) {

  file << ch;
  i++;
       if(i == counter){
           file << dot;
           i = 0;
       }
    }
  file.close();
  fin.close();
 return 0;
}

谢谢大家的回复。

【问题讨论】:

  • 注意它应该是if(i == counter),否则你将简单地将counter分配给i。
  • char dot = "."; - 这是一个奇怪的隐式转换......
  • 我的项目代码与我写的不同(我做了一些“修复”应该可以缓解生活,但我犯了错误),感谢您指出,修复了这些。

标签: c++ file insert char


【解决方案1】:

对于像这样的简单应用程序,在开始阅读之前打开输出文件,并且在完成之前不要关闭它。如所写,每次读取字符时都会打开输出文件,然后覆盖之前文件中的任何内容。您可以以追加模式打开文件以在最后粘贴新数据,但保持打开状态更简单(也更快)。

【讨论】:

  • 感谢您的指出。对于那些可能想知道为什么我需要这样的程序的人,我可以说我用它来分隔十六进制代码。
【解决方案2】:

每次向输出文件写入内容时,打开它,写入输出,然后关闭它。由于您打开文件的方式,您的每次写入都从文件的开头开始。

如果相反,您将输出文件保持打开状态,直到完成所有数据的写入,那么下一次写入将在前一次写入结束的点继续,产生您期望的输出序列。

    ofstream file;
    file.open("file2.txt");

    while (fin >> noskipws >> ch) {
        file << ch;
        i++;
        if (i == counter) {
            file << dot;
            i = 0;
        }
    }

【讨论】:

    【解决方案3】:

    刚开始打开文件,更新它,最后关闭所有文件。

     ofstream file;               <------------+
     file.open ("file2.txt");                  |
                                               |
    while (fin >> noskipws >> ch) {            |
                                               |
    //ofstream file;           ---+            |
                                  +----->------+
    //file.open ("file2.txt"); ---+
    
      file << ch;
    //file.close();
      i++;
           if(i == counter){
           //file.open ("file2.txt");
             file << dot;
           //file.close();
            i=0;
           }
        }
    //Close files
        file.close(); 
        fin.close() ;
    

    【讨论】:

      【解决方案4】:

      当您在循环中写入文件时,您很可能希望在循环之外打开文件。通常,当您打开文件进行写入时,旧的内容将被覆盖。

      这样做:

      ofstream file ("file2.txt")
      while (...)
      {
         ...
         file << ....
         ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        • 2011-10-19
        • 2011-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多