为什么我的代码只打印最后一行到txt文件。 [英] Why is my code only printing last line to txt file.

查看:80
本文介绍了为什么我的代码只打印最后一行到txt文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用cout时。我得到了所有的信息。当我使用ofstream out()时,它只打印最后一行。



我尝试过:



When I use cout. I get all the information. When I use ofstream out("") it only prints the last line.

What I have tried:

#include<iostream> //i.o
#include<cctype> //for isdigit
#include<sstream> //to phrase string
#include<string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include<bits/stdc++.h>
#include<iostream> //i.o
#include<cctype> //for isdigit
#include<sstream> //to phrase string
#include<string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

void splitString(string str)
{ofstream out("example.txt");
    string alpha, num, special;
    for (int i=0; i<str.length(); i++)
    {

         if((str[i] >= 'A' && str[i] <= 'Z') ||
                (str[i] >= 'a' && str[i] <= 'z'))
           break;

           else if (isdigit(str[i]))
            num.push_back(str[i]);
    }


    out << num<<endl;

}

// Driver code
int main()
{

std::string word;
    std::vector<std::string> file;

    std::ifstream in( "Files.txt" );

    while ( in >> word )
        file.push_back( word );

    for ( size_t i=0; i<file.size(); i++ ){




    string str = file.at(i);
    splitString(str);}
    return 0;
}

推荐答案

这是因为你正在创建一个新的 ofstream 每次调用 splitString 时对象。因此它打开文件并只写入函数中提取的数据。您应该在 main 函数中创建流,并将引用传递给 splitString 作为第二个参数。或者,在 main 中写入所有文件,并使 splitString 返回它创建的字符串。
It is because you are creating a new ofstream object every time you call splitString. So it opens the file and writes just the data that is extracted in the function. You should create the stream n your main function, and pass the reference to splitString as a second parameter. Alternatively, do all the file writing in main, and make splitString return the string that it creates.


string alpha, num, special;
...
num.push_back(str[i]);



上次检查时, .push_back 与向量有关,而不是蜇。可能是没有得到预期结果的原因。


您的代码没有按照您的预期行事,您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么和你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


Last time I checked, .push_back is related to vectors, not stings. Probably the reason to not get the expected result.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于为什么我的代码只打印最后一行到txt文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆