首页 \ 问答 \ 如何一行一行或一整个文本文件读取?(How to read line by line or a whole text file at once?)

如何一行一行或一整个文本文件读取?(How to read line by line or a whole text file at once?)

我在一个介绍文件的教程(如何从\到文件的读写)

首先,这不是一个功课,这只是我正在寻求的一般帮助。

我知道如何一次阅读一个字,但我不知道如何一次读一行或如何阅读整个文本文件。

如果我的文件包含1000个单词怎么办? 阅读每个单词是不切实际的。

我的文本文件(Read)包含以下内容:

我喜欢玩游戏我喜欢阅读我有2本书

这是我迄今为止所完成的:

#include <iostream>
#include <fstream>

using namespace std;
int main (){

  ifstream inFile;
  inFile.open("Read.txt");

  inFile >>

是否有任何可能的方式一次阅读整个文件,而不是阅读每一行或每个单词分开?


I'm in a tutorial which introduces files (how to read and write from\to file)

First of all, this is not a homework, this is just general help I'm seeking.

I know how to read one word at a time, but I don't know how to read one line at a time or how to read the whole text file.

What if my file contains 1000 words? It is not practical to read each word.

My text file named (Read) contains the following:

I love to play games I love reading I have 2 books

This is what I have accomplished so far:

#include <iostream>
#include <fstream>

using namespace std;
int main (){

  ifstream inFile;
  inFile.open("Read.txt");

  inFile >>

Is there any possible way to read the whole file at once, instead of reading each line or each word separate?


原文:https://stackoverflow.com/questions/13035674
更新时间:2023-12-13 15:12

最满意答案

这是实现这一目标的另一种方式,请检查是否符合您的要求

这是小提琴: http : //codepen.io/anon/pen/dOxyyd

var parksListJSON = {
    "for": "abcde",
    "srn": "12345678",
    "cohort": "CS/IT/JH6",
    "modules": {
        "uh6com1051": {
            "name": "Scripting"
        },
        "uh6com1063": {
            "name": "UX"
        },
        "uh6com1099": {
            "name": "Project"
        }
    },
    "schedule": [{
        "module": "uh6com1051",
        "sessionType": "lab",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "mon",
            "location": "E406",
            "time": "11"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "tues",
            "location": "E150",
            "time": "11"
        }]
    }, {
        "module": "uh6com1051",
        "sessionType": "tutorial",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "weds",
            "location": "C402",
            "time": "9"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "thurs",
            "location": "C402",
            "time": "9"
        }]
    }, {
        "module": "uh6com1051",
        "sessionType": "lecture",
        "duration": 1,
        "day": "thurs",
        "location": "B400",
        "time": "15"
    }, {
        "module": "uh6com1063",
        "sessionType": "lab",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "tues",
            "location": "E407",
            "time": "11"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "weds",
            "location": "E150",
            "time": "9"
        }, {
            "group": "C",
            "day": "thurs",
            "location": "E150",
            "time": "11"
        }]
    }, {
        "module": "uh6com1063",
        "sessionType": "tutorial",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "mon",
            "location": "C402",
            "time": "13"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "thurs",
            "location": "C400",
            "time": "9"
        }, {
            "group": "C",
            "day": "fri",
            "location": "C400",
            "time": "9"
        }]
    }, {
        "module": "uh6com1063",
        "sessionType": "lecture",
        "duration": 1,
        "day": "tues",
        "location": "A166",
        "time": "12"
    }, {
        "module": "uh6com1099",
        "sessionType": "tutorial",
        "duration": 1,
        "allocatedTime": {
            "group": "D",
            "day": "weds",
            "location": "LB252",
            "time": "11"
        },
        "alternativeTimes": [{
            "group": "A",
            "day": "mon",
            "location": "LB252",
            "time": "13"
        }, {
            "group": "B",
            "day": "mon",
            "location": "LB252",
            "time": "16"
        }, {
            "group": "C",
            "day": "tues",
            "location": "LB252",
            "time": "11"
        }, {
            "group": "E",
            "day": "thurs",
            "location": "LB252",
            "time": "11"
        }]
    }, {
        "module": "uh6com1099",
        "sessionType": "lecture",
        "duration": 1,
        "day": "fri",
        "location": "A161",
        "time": "12"
    }]
}

function displaySchedule(schedule) {
    var scheduleElement = document.getElementsByClassName("schedule");
    for (var i = 0; i < schedule.length; ++i) {
        var cellElement;
        if (schedule[i].hasOwnProperty("allocatedTime")) {
            var allocatedTime = schedule[i].allocatedTime;
            cellElement = document.querySelector("." + allocatedTime.day + " .s" + allocatedTime.time);
            cellElement.innerHTML = getModuleText(schedule[i], allocatedTime);
        }
        if (schedule[i].hasOwnProperty("alternativeTimes")) {
            for (var j = 0; j < schedule[i].alternativeTimes.length; ++j) {
                var alternativeTime = schedule[i].alternativeTimes[j];
                cellElement = document.querySelector("." + alternativeTime.day + " .s" + alternativeTime.time);
                if (cellElement.innerHTML.length > 0) {
                    cellElement.innerHTML += "<br>";
                }
                cellElement.innerHTML += getModuleText(schedule[i], alternativeTime);
            }
        }
    }
}

// Helper function
function getModuleText(schedule, scheduleTime) {
    var text = "<div class='module'>" + 
        "<span class='title'>Mod: </span>" + schedule.module + "<br>" + 
        "<span class='title'>Type: </span>" + schedule.sessionType + "<br>" + 
        "<span class='title'>Loc: </span>" + scheduleTime.location + "<br>" + 
        "<span class='title'>Group: </span>" + scheduleTime.group + 
        "</div>";
    return text;
}

displaySchedule(parksListJSON.schedule);
/* Styles used from http://www.w3schools.com/css/css_table.asp . Check that link for info */

table.schedule {
    border-collapse: collapse;
    font-family: "Segoe UI", Arial, sans-serif;
}

.schedule th {
    background-color: #4CAF50;
    color: white;
}
.schedule th, .schedule td {
    padding: 25px;
    border-bottom: 1px solid #ddd;
}

.schedule tr:hover {
    background-color: #f5f5f5
}

.schedule tr:nth-child(even) {
    background-color: #f2f2f2
}

.schedule .dayRowHead, .schedule .title{
    font-weight: 500;
}

.schedule .module{
    border: 1px solid #ddd;
    padding: 10px;
}
<!-- Changed id to class -->

<table class='schedule'>
    <thead>
        <!-- Row 0-->
        <tr> 
            <th>Day</th>
            <th>09:00</th>
            <th>10:00</th>
            <th>11:00</th>
            <th>12:00</th>
            <th>13:00</th>
            <th>14:00</th>
            <th>15:00</th>
            <th>16:00</th>
            <th>17:00</th>
        </tr>
    </thead>
    <tbody>
        <!-- Row 1-->
        <tr class="mon">
            <td class="dayRowHead">Mon</td>
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
        <!-- Row 2-->
        <tr class="tues">
             <td class="dayRowHead">Tue</td>
             <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
        <!-- Row 3-->
        <tr class="weds">
            <td class="dayRowHead">Wed</td>
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr> 
        <!-- Row 3-->
        <tr class="thurs">
            <td class="dayRowHead">Thu</td> 
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
        <!-- Row 4-->
        <tr class="fri">
            <td class="dayRowHead">Fri</td>
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
    </tbody>
</table>


This is another way to achieve the same, check if suits your requirements

Here is the fiddle: http://codepen.io/anon/pen/dOxyyd

var parksListJSON = {
    "for": "abcde",
    "srn": "12345678",
    "cohort": "CS/IT/JH6",
    "modules": {
        "uh6com1051": {
            "name": "Scripting"
        },
        "uh6com1063": {
            "name": "UX"
        },
        "uh6com1099": {
            "name": "Project"
        }
    },
    "schedule": [{
        "module": "uh6com1051",
        "sessionType": "lab",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "mon",
            "location": "E406",
            "time": "11"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "tues",
            "location": "E150",
            "time": "11"
        }]
    }, {
        "module": "uh6com1051",
        "sessionType": "tutorial",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "weds",
            "location": "C402",
            "time": "9"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "thurs",
            "location": "C402",
            "time": "9"
        }]
    }, {
        "module": "uh6com1051",
        "sessionType": "lecture",
        "duration": 1,
        "day": "thurs",
        "location": "B400",
        "time": "15"
    }, {
        "module": "uh6com1063",
        "sessionType": "lab",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "tues",
            "location": "E407",
            "time": "11"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "weds",
            "location": "E150",
            "time": "9"
        }, {
            "group": "C",
            "day": "thurs",
            "location": "E150",
            "time": "11"
        }]
    }, {
        "module": "uh6com1063",
        "sessionType": "tutorial",
        "duration": 1,
        "allocatedTime": {
            "group": "A",
            "day": "mon",
            "location": "C402",
            "time": "13"
        },
        "alternativeTimes": [{
            "group": "B",
            "day": "thurs",
            "location": "C400",
            "time": "9"
        }, {
            "group": "C",
            "day": "fri",
            "location": "C400",
            "time": "9"
        }]
    }, {
        "module": "uh6com1063",
        "sessionType": "lecture",
        "duration": 1,
        "day": "tues",
        "location": "A166",
        "time": "12"
    }, {
        "module": "uh6com1099",
        "sessionType": "tutorial",
        "duration": 1,
        "allocatedTime": {
            "group": "D",
            "day": "weds",
            "location": "LB252",
            "time": "11"
        },
        "alternativeTimes": [{
            "group": "A",
            "day": "mon",
            "location": "LB252",
            "time": "13"
        }, {
            "group": "B",
            "day": "mon",
            "location": "LB252",
            "time": "16"
        }, {
            "group": "C",
            "day": "tues",
            "location": "LB252",
            "time": "11"
        }, {
            "group": "E",
            "day": "thurs",
            "location": "LB252",
            "time": "11"
        }]
    }, {
        "module": "uh6com1099",
        "sessionType": "lecture",
        "duration": 1,
        "day": "fri",
        "location": "A161",
        "time": "12"
    }]
}

function displaySchedule(schedule) {
    var scheduleElement = document.getElementsByClassName("schedule");
    for (var i = 0; i < schedule.length; ++i) {
        var cellElement;
        if (schedule[i].hasOwnProperty("allocatedTime")) {
            var allocatedTime = schedule[i].allocatedTime;
            cellElement = document.querySelector("." + allocatedTime.day + " .s" + allocatedTime.time);
            cellElement.innerHTML = getModuleText(schedule[i], allocatedTime);
        }
        if (schedule[i].hasOwnProperty("alternativeTimes")) {
            for (var j = 0; j < schedule[i].alternativeTimes.length; ++j) {
                var alternativeTime = schedule[i].alternativeTimes[j];
                cellElement = document.querySelector("." + alternativeTime.day + " .s" + alternativeTime.time);
                if (cellElement.innerHTML.length > 0) {
                    cellElement.innerHTML += "<br>";
                }
                cellElement.innerHTML += getModuleText(schedule[i], alternativeTime);
            }
        }
    }
}

// Helper function
function getModuleText(schedule, scheduleTime) {
    var text = "<div class='module'>" + 
        "<span class='title'>Mod: </span>" + schedule.module + "<br>" + 
        "<span class='title'>Type: </span>" + schedule.sessionType + "<br>" + 
        "<span class='title'>Loc: </span>" + scheduleTime.location + "<br>" + 
        "<span class='title'>Group: </span>" + scheduleTime.group + 
        "</div>";
    return text;
}

displaySchedule(parksListJSON.schedule);
/* Styles used from http://www.w3schools.com/css/css_table.asp . Check that link for info */

table.schedule {
    border-collapse: collapse;
    font-family: "Segoe UI", Arial, sans-serif;
}

.schedule th {
    background-color: #4CAF50;
    color: white;
}
.schedule th, .schedule td {
    padding: 25px;
    border-bottom: 1px solid #ddd;
}

.schedule tr:hover {
    background-color: #f5f5f5
}

.schedule tr:nth-child(even) {
    background-color: #f2f2f2
}

.schedule .dayRowHead, .schedule .title{
    font-weight: 500;
}

.schedule .module{
    border: 1px solid #ddd;
    padding: 10px;
}
<!-- Changed id to class -->

<table class='schedule'>
    <thead>
        <!-- Row 0-->
        <tr> 
            <th>Day</th>
            <th>09:00</th>
            <th>10:00</th>
            <th>11:00</th>
            <th>12:00</th>
            <th>13:00</th>
            <th>14:00</th>
            <th>15:00</th>
            <th>16:00</th>
            <th>17:00</th>
        </tr>
    </thead>
    <tbody>
        <!-- Row 1-->
        <tr class="mon">
            <td class="dayRowHead">Mon</td>
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
        <!-- Row 2-->
        <tr class="tues">
             <td class="dayRowHead">Tue</td>
             <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
        <!-- Row 3-->
        <tr class="weds">
            <td class="dayRowHead">Wed</td>
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr> 
        <!-- Row 3-->
        <tr class="thurs">
            <td class="dayRowHead">Thu</td> 
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
        <!-- Row 4-->
        <tr class="fri">
            <td class="dayRowHead">Fri</td>
            <td class="s9"></td>
            <td class="s10"></td>
            <td class="s11"></td>
            <td class="s12"></td>
            <td class="s13"></td>
            <td class="s14"></td>
            <td class="s15"></td>
            <td class="s16"></td>
            <td class="s17"></td>
        </tr>
    </tbody>
</table>

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)