程序在win下无法运行,但在mac上运行。为什么?

发布于 2024-11-16 13:23:47 字数 1027 浏览 3 评论 0原文

我的大学作业有问题。该程序需要将文本从一行文本编辑为每行不超过80个符号的段落。你只能在空格、'.'、'、'等之后进行换行。

这段代码在mac上可以正常工作,但在我的win7上不起作用。有人可以帮助我吗?

#include <fstream>
using namespace std;

int main() {
    int i=0;
    char symbol[80];
    fstream fin ("in.txt", ios::in);
    fstream fout ("out.txt", ios::out);
    fin.get(symbol[i]);
    while(!fin.eof()){
        if(symbol[i] == '\n' || i == 80) {
            while(symbol[i] != ' ' && symbol[i] != ',' && symbol[i] !='?' && symbol[i] != '.' && symbol[i] !='!' && symbol[i]!='(' && symbol[i] !=')'){
                i--;
                fin.seekg(-1, ios::cur);
            }
            for(int j=0;j<i;j++){
                fout.put(symbol[j]);
            }
            fout.put('\n');
            i = -1;

        }
        i++;
        fin.get(symbol[i]);
    }
    if (fin.eof()){
        for(int j=0;j<i;j++){
            fout.put(symbol[j]);
        }
    }
    fin.close();
    fout.close();
    return 0;
}

I have problem with my university homework. The program which needs to edit text from one line text to a paragraph where in each line is not more than 80 symbols. You can make a line break only after space, '.', ',' etc.

This code is working properly on mac, but it doesn't work on my win7. Can anyone help me?

#include <fstream>
using namespace std;

int main() {
    int i=0;
    char symbol[80];
    fstream fin ("in.txt", ios::in);
    fstream fout ("out.txt", ios::out);
    fin.get(symbol[i]);
    while(!fin.eof()){
        if(symbol[i] == '\n' || i == 80) {
            while(symbol[i] != ' ' && symbol[i] != ',' && symbol[i] !='?' && symbol[i] != '.' && symbol[i] !='!' && symbol[i]!='(' && symbol[i] !=')'){
                i--;
                fin.seekg(-1, ios::cur);
            }
            for(int j=0;j<i;j++){
                fout.put(symbol[j]);
            }
            fout.put('\n');
            i = -1;

        }
        i++;
        fin.get(symbol[i]);
    }
    if (fin.eof()){
        for(int j=0;j<i;j++){
            fout.put(symbol[j]);
        }
    }
    fin.close();
    fout.close();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2024-11-23 13:23:47

您没有指定“它不起作用”的方式,并且我没有研究您的代码(看起来您没有花时间将其减少到重现问题所需的最低限度,所以 TL;DR)。鉴于您提到的平台,您很可能遇到换行问题

在 Microsoft 平台上,换行符是回车符(CR0x0D\r),后跟换行符(LF) , 0x0A\n)。你只是在寻找 LF。

(据推测,从前,微软的磁盘操作系统在机械打字机上运行,​​操作员必须将笔架返回到开头,然后才能移动到下一行)。

You do not specify how "it doesn't work" and I haven't studied your code (It doesn't look like you've taken the time to reduce it to the minimum required to reproduce your problem, so TL;DR). You could well be experiencing a Newline problem, given the platforms you mention..

On Microsoft platforms, a new-line is Carriage Return (CR, 0x0D or \r) followed by Line Feed (LF, 0x0A or \n). You're only looking for LF.

(Presumably, once-upon-a-time, MicroSoft's Disk Operating System ran on mechanical typewriters and the operator would have to return the carriage to the beginning before moving to the next line).

幽梦紫曦~ 2024-11-23 13:23:47

这段代码中的“fseek”和其他几个部分都是类似 UNIX 的操作。
Windows 不是类 Unix 机器(而 Mac 是)。

如果您在计算机上安装 cygwin,它会添加这些内容,并且可能会为您解决问题。

或者,您可能会遇到这个问题:
http://cboard.cprogramming.com/c-programming /129839-fseek-broken-under-windows.html

不过,我们在这里都在猜测,因为您没有提供足够的信息让我们真正了解问题所在你真的有。

"fseek" and several other bits and pieces in this code are unix-like operations.
Windows is not a unix-like machine (whereas Mac is).

If you install cygwin on your machine it will add those and may just solve your problem for you.

Alternatively, you could be suffering from this problem:
http://cboard.cprogramming.com/c-programming/129839-fseek-broken-under-windows.html

We're all kind of guessing here, though, as you haven't provided enough information for us to actually know what problem you're really having.

清风疏影 2024-11-23 13:23:47

我在代码中看到的另一个问题(可能是也可能不是问题的原因):
如果给定行中只有普通字符(没有空格、问号、感叹号等)怎么办?

然后,如果我正确地阅读了这篇文章,您最里面的 while 循环将超出字符串的开头并开始处理一些垃圾。此外,您可能会错误地在文件流中进行搜索。

Another issue that I see in the code (which might or might not be the cause of the problem):
What if in a given line you have only normal characters (no spaces, questions, exclamantion marks etc)?

Then, if I am reading this correctly, your innermost while-loop will go beyond the beginning of your string and start processing some garbage. Also, as a result, you might incorrectly seekg in your file stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文