C++ 试图在C+;中将一个文本文件拆分为多个文本文件+;

C++ 试图在C+;中将一个文本文件拆分为多个文本文件+;,c++,C++,我试图让这段代码将一个输入文件拆分为两个文件。我希望将代码拆分,以便一个新文件包含所有奇数字符,另一个文件包含所有偶数字符。我的代码没有错误,它生成了两个新文件,但这两个新文件中没有任何内容。我想知道我的代码出了什么问题(我确信它有很多错误)。我对编程还是相当陌生的 #include <iostream> #include <fstream> #include <string> using namespace std; void split(char sou

我试图让这段代码将一个输入文件拆分为两个文件。我希望将代码拆分,以便一个新文件包含所有奇数字符,另一个文件包含所有偶数字符。我的代码没有错误,它生成了两个新文件,但这两个新文件中没有任何内容。我想知道我的代码出了什么问题(我确信它有很多错误)。我对编程还是相当陌生的

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void split(char sourceFile[], char destFile1[], char destFile2[]) {
    int chars = 0;
    ifstream sFile;
    sFile.open(sourceFile);
    ofstream file1;
    file1.open(destFile1);
    ofstream file2;
    file2.open(destFile2);

    while (!sFile.eof()) {
        sFile.read(sourceFile + chars, 1);

        cout << sourceFile[chars];
        if (chars % 2 == 0) {
            file1 << sourceFile[chars];
        } else {
            file2 << sourceFile[chars];
        }
        chars++;


    }
}

int main() {
    split("text.txt", "one.txt", "two.txt");
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效拆分(char sourceFile[],char destFile1[],char destFile2[]{
int chars=0;
ifsfile;
打开(源文件);
流文件1;
file1.打开(destFile1);
流文件2;
file2.打开(destFile2);
而(!sFile.eof()){
读取(源文件+字符,1);

cout缩进有误导性!file close()调用在循环中


而且:
i%2==5
从来都不是真的。如果(i%2==0){…}或者{…}

你忘记了一个“;”并且拼错了“sourcFile”怎么办

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void split(char sourceFile[], char destFile1[], char destFile2[])
{
int chars = 0;

ifstream sFile;
sFile.open (sourceFile);

ofstream file1;
file1.open (destFile1);

ofstream file2;
file2.open (destFile2);

while (!sFile.eof())
{
    sFile.read(sourceFile+chars,1); // forgot ';'

    cout << sourceFile[chars];
    if (chars % 2 == 0)
    {
        file1<< sourceFile[chars];
    }
    else if(chars % 2 == 5)
    {
        file2 << sourceFile[chars]; // misspelled 'sourcFile'
    }
    chars++;

sFile.close();
file1.close();
file2.close();
}

}

int main()
{
split("text.txt", "one.txt", "two.txt");
return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效拆分(char sourceFile[],char destFile1[],char destFile2[]
{
int chars=0;
ifsfile;
sFile.open(源文件);
流文件1;
file1.open(destFile1);
流文件2;
file2.open(destFile2);
而(!sFile.eof())
{
sFile.read(sourceFile+chars,1);//忘记';'

cout它有很多问题(如你所说)

1) 所有文件都是
ifstream
,如果要输出到文件,请使用流的

2) 您不应尝试将文件名用作代码的变量。只需声明一个新的char变量即可

3) 使用get not read读取单个字符

4) 测试文件是否正确结束

5) 不要关闭循环中的文件,事实上根本不要关闭文件,退出函数时会自动关闭

6)
chars%2==5
到底是关于什么的?这永远不会是真的

把这些放在一起

char ch;
while (sFile.get(ch))
{
    if (chars % 2 == 0)
        file1 << ch;
    else
        file2 << ch;
    chars++;
}
charch;
while(sFile.get(ch))
{
如果(字符%2==0)

file1不是对问题的回答,而是实现所需目标的程序的紧凑版本:

#include <algorithm>
#include <iterator>
#include <fstream>
#include <functional>

int main()
{
    typedef std::ostreambuf_iterator<char> oit;
    unsigned int chars(0);
    std::for_each(std::istreambuf_iterator<char>(
                      std::ifstream("text.txt") >> std::noskipws),
                  std::istreambuf_iterator<char>(),
                  std::bind([=](oit _1, oit _2, char c) mutable {
                          *(chars++ % 2? _1: _2)++ = c; },
                      oit(std::ofstream("one.txt") << std::dec),
                      oit(std::ofstream("two.txt") << std::dec),
                      std::placeholders::_1));
}
#包括
#包括
#包括
#包括
int main()
{
typedef std::ostreambuf_迭代器oit;
无符号整数字符(0);
std::for_each(std::istreambuf_迭代器(
std::ifstream(“text.txt”)>>std::noskipws),
std::istreambuf_迭代器(),
std::bind([=](oit\u 1,oit\u 2,char c)可变{
*(chars++%2?_1:_2++=c;},

oit(std::of Stream(“one.txt”)一些非常严重的问题:

  • 你认为循环控制毫无意义,它没有很好的定义 当“istream::eof()”变为true,但按字节读取时 字节,当您执行此操作时,它可能会导致您输入 比你想要的多循环一次

  • 一个相关的问题是,您没有验证 成功。读取的成功应该是您的循环控制

  • 您正在读取未定义的字符文字 行为(并将在许多系统上崩溃);足够 阅读,你在阅读文字的结尾,进入 未定义的内存。您真的应该为内存使用本地缓冲区 阅读

  • 你不做任何错误检查。至少,你 应验证您已成功打开文件,并且 应关闭输出文件,并在 关闭,以确保写入工作正常

我可能会使用类似的方法:

void
split( std::string const& source, 
       std::string const& dest1,
       std::string const& dest2 )
{
    std::istream in( source.c_str() );
    if ( ! in.is_open() ) {
        //  Cannot open source...
    }
    std::ostream out1( source.c_str() );
    if ( ! out1.is_open() ) {
        //  Cannot create dest1
    }
    std::ostream out2( source.c_str() );
    if ( ! out2.is_open() ) {
        //  Cannot create dest2
    }
    std::ostream* currentOut = &out1;
    std::ostream* otherOut = &out2;
    char ch;
    while ( in.get( ch ) ) {
        currentOut->put( ch );
        std::swap( currentOut, otherOut );
    }
    out1.close();
    if ( !out1 ) {
        //  Write error on dest1...
    }
    out2.close();
    if ( !out2 ) {
        //  Write error on dest2...
    }
}
(如您所见,执行复制的实际循环是 非常简单。大部分代码都有错误
处理。)

你知道这永远不会是真的,是吗:
chars%2==5
再次阅读OP;“我的代码没有错误(…)我想知道我的代码有什么问题(我确信它有很多错误)”。这里所说的一切都是一个实际问题。所有其他问题都已经得到解决。一个问题是,发布的代码当然不是真正的代码。OP声称代码是经过编译的,因此缺少分号和错拼是因为他键入了代码,而不是剪切粘贴。没有考虑到这种可能性。他只是试图lp.我应该删除这篇文章吗(我在SO还是个新手)?@MartenBE我不担心。不幸的是,人们不发布他们遇到问题的实际代码太常见了。你必须明确关闭输出文件,因为你必须在关闭后检查它们的状态,以确保所有内容都正确编写。顺便说一句,感谢所有的帮助。我终于找到了答案是的。在你们帮了我这么多之后,它仍然不起作用。结果是我把我的测试程序拼错了:/我的版本更紧凑:-)(只是它做了一些错误检查)。然而,一个优雅的解决方案是使用一个在两个文件之间切换的特殊输出迭代器,然后使用
std::copy
@JamesKanze:好吧,您的解决方案也有不同的功能:我的理解是,基于
char
s值,它应该发送到不同的目标。查看当前状态不过,这个问题并不完全正确。我会解决这个问题…@JamesKanze:使用自定义迭代器会非常复杂。显然可行,但我认为这需要更多的代码,最终可能会变得可读。我更喜欢在回答明显的家庭作业时使用有趣的技术(如果这不是家庭作业,我想我的技术使用了一些有趣的东西来学习)。从头开始的自定义迭代器需要很多代码。(尽管输出迭代器比其他迭代器容易得多。)但使用Boost::迭代器可能会使它成为一个可行的解决方案