解决使用{freopen与 getline}读取不同文件时产生的的问题

读取单一文件

使用 freopen重定向。

用 getline逐行读取,处理。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
string s;

int main()
{
    freopen("text1.in", "r", stdin);
    while (getline(cin, s)) {
        /* - code - */
    }
    fclose(stdin);
    return 0;
}

读取多个文件

基本方法同上。

特别注意:在读取完一个文件后,

使用 cin.clear()清空输入流。

否则可能会有 除第一个文件,其他文件无读入的现象。

2020-1-27-1:07 在读取处理两个 txt文本数据时产生该问题。

编译环境:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=e:/tio/dev-cpp/mingw32/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC)

编译命令:
g++ Name.cpp -o Name.exe

正确示例:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
string s;

int main()
{
    freopen("text1.in", "r", stdin);
    while (getline(cin, s)) {
        /* - code - */
    }
    fclose(stdin);
    cin.clear();
    freopen("text2.in", "r", stdin);
    while(getline(cin, s)) {
        /* - code - */
    }
    fclose(stdin);
    // ...
    return 0;
}

原文地址:https://www.cnblogs.com/leprechaun-kdl/p/12235322.html

时间: 2024-08-28 22:14:17

解决使用{freopen与 getline}读取不同文件时产生的的问题的相关文章

【 D3.js 进阶系列 — 1.2 】 读取 CSV 文件时乱码的解决方法

在 D3 中使用 d3.csv 读取 CSV 文件时,有时会出现乱码问题.怎么解决呢? 1. 乱码问题 使用 d3.csv 读取 xxx.csv 文件时,如果 xxx.csv 文件使用的是 UTF-8 编码,不会有什么问题.当然,个人认为尽量使用 UTF-8 编码,可以在同一编码内使用各国文字. 但是,如果 xxx.csv 文件使用的是 utf-8 编码,使用 Microsoft Excel 打开的时候,可能会出现乱码,因为国内的 Excel 默认使用 GB2312 打开,而且在打开的时候不能选

spring使用@Value注解读取.properties文件时出现中文乱码问题的解决

解决办法 在spring中我们常常使用.properties对一些属性进行一个提前配置, spring 在读取*.properties文件时, 默认使用的是asci码, 这时 我们需要对其编码进行转换. 下面列举两种常见的方法. 方法一:在配置spring.xml文件时,声明所需的∗.properties文件时直接使用"utf−8"编码 <context:property-placeholder location="classpath:conf/*.properties

使用OpenCV的VideoCapture 读取.mp4文件时出现以下错误:Unable to stop the stream: Inappropriate ioctl for device

使用OpenCV的VideoCapture 读取.mp4文件时出现以下错误:Unable to stop the stream: Inappropriate ioctl for device 此问题由于未安装ffmpeg导致. sudo apt-get install ffmpeg 重新编译opecv即可. cd xx/opecv/build rm -rf * cmake ../                                //执行cmake ../后打印信息中video I/

Java程序中读取外部文件时的路径问题

转自:https://www.cnblogs.com/wt20/p/8320346.html 项目经常会读取一些配置文件, 因此getResource方法便能够起到重要作用 使用时主要是两种方法, 一个是字节码文件Class类, 另一个是ClassLoader类加载器 使用Class类时有两种使用方式: 1. 使用"/"  获取到的是classpath路径 2. 不使用"/" 这就是相对路径 ClassLoader类 没有"/"的写法, 获取到的

记录一次读取hdfs文件时出现的问题java.net.ConnectException: Connection refused

公司的hadoop集群是之前的同事搭建的,我(小白一个)在spark shell中读取hdfs上的文件时,执行以下指令 >>> word=sc.textFile("hdfs://localhost:9000/user/hadoop/test.txt") >>> word.first() 报错:java.net.ConnectException: Call From hadoop/133.0.123.130 to localhost:9000 fail

解决pathForResource返回nil, 无法读取plist文件问题

有很多人在设置plist文件的时候, 会发现读取不了plist文件里面的内容, 返回值为nil, 下面我们来解决一下这个问题. 首先我们打开工程并且按照下面的步骤来设置: 设置好后, 我们来写一段代码测试一下看看是否添加好: NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"images" ofType:@"plist"]; _imageData 

读取大文件时的优化经验

最近在编写一个关于图形学的东西时,由于需要读取模型,写了一个obj文件和mtl文件解析器.实际调试时,由于该文件较长,比如obj文件达到了20万行的量级,在解析时凸显出了各种性能问题,解决这些性能问题的同时,也总结出了一些经验,记录如下: 1 必须使用缓冲区.虽然操作系统实现读取文件应该是有缓冲区概念的,但是结果显示如果不使用缓冲区,而用fgetc挨个字符进行读取,速度会比使用缓冲区慢上1个数量级.因此,引出第一条经验:一切大文件读取必须使用缓冲区,减少fread或fgetc的次数. 2 关于m

如何解决jsp:include标签在包含html文件时遇到的乱码问题

在一个JSP页面中,常常需要包含另一个文件,JSP为我们提供了jsp:include标签可以完成这个功能,比如:<jsp:include page="some.jsp"></jsp:include>,在自定义Tag中,我们可以通过pageContext.include(file);的方式来实现和jsp:include同样的效果.但是如果被包含的是一个html文件,我们就很有可能遇到乱码的问题,比如本来的jsp页面中使用了UTF-8编码,html中的内容也采用了U

java读取视频文件时长

1.下载jar包:http://www.sauronsoftware.it/projects/jave/index.php 2.上代码 1 @RequestMapping(value = "amendFile.htm", produces = "application/json;charset=UTF-8") 2 @ResponseBody 3 public String amendFile(MultipartFile file, HttpServletReques