如何一行一行或一整个文本文件读取?(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
最满意答案
这是实现这一目标的另一种方式,请检查是否符合您的要求
这是小提琴: 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>
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
这是实现这一目标的另一种方式,请检查是否符合您的要求 这是小提琴: http : //codepen.io/anon/pen/dOxyyd var parksListJSON = { "for": "abcde", "srn": "12345678", "cohort": "CS/IT/JH6", "modules": { "uh6com1051": { "name": "Scripting" }, ...
-
最有可能的问题发生是因为您试图解析关键posts的值两次 ,一次在PostData ,一次在ViewController 。 首先在Swift 3中,一个JSON字典是[String:Any] ,其次 - 正如我的评论中已经提到的那样 - 私人支持变量在Swift中是无稽之谈。 PostData类可以减少到 class PostData { let profileImageURL : String let fullName : String let location : String ...
-
如何在UITableview单元格中正确显示分离的JSON数据?(How to display JSON data seperated correctly in UITableview cells?)[2021-12-29]
编辑: 一些示例代码供您使用。 // JSONLoader.m - (NSArray *)locationsFromJSONFile:(NSURL *)url { // Existing code ....... // Get an array of dictionaries with the key "locations" NSArray *array = [jsonDictionary objectForKey:@"locations"]; // Iterate t ... -
对不起,我对jQuery并不熟悉,所以我在一些地方使用了原生JavaScript。 如果您无法从服务器端访问格式,则可以在客户端更改格式,如下所示: $(document).ready(function() { var json = [{ "Supplier": "Supplier1", "Product": "Oxygen", "Quantity": 50 }, { "Supplier": "Supplier1", ...
-
发生的事情是getData有一个在后台运行的完成块,你需要在读完返回的数据后告诉swift更新表,但是你需要在主线程中发回这个更新: func getData(resultHandler: (data: AnyObject?) -> ()) -> () { Alamofire.request(.GET, "http://testurl/api/", parameters: ["groupname": "\(group!)"]) .responseJSON { (_, _, JSON, _) in ...
-
带有大JSON的简单ngTable,不会在单元格中显示任何数据(Simple ngTable with large JSON not showing any data in cells)[2024-02-18]
您可以使用$http而不是$resource 。 您的响应数据将位于响应的data属性中。 请尝试以下方法: $scope.tableParams = new ngTableParams({}, { getData: function(params) { var varApiGet = $http.get(params.url()).then(function(response) { var data = response.data; params.total( ... -
您可以使用jQuery来执行此操作。 假设您的所有数据都进入xx和yy这两行,只需设置一个包含行xx和yy的表。 表格单元格中不需要div 。 然后,您可以使用jQuery在适当的行中插入每个数据点的td 。 这是一个例子:
-
我们可以使用来自tidyr gather library(tidyr) gather(df1, Parameter, Proportion) # Parameter Proportion # (fctr) (dbl) #1 HomeCondn_Good 65.9 #2 HomeCondn_Livabl 29.7 #3 HomeCondn_Dilapdtd 4.3 We can use gath ...
-
滚动需要方法tableView:cellForRowAtIndexPath ,因为tableView不会一次加载所有单元格。 如果只想重新加载tableView的一部分,可以使用方法reloadRowsAtIndexPaths:withRowAnimation:或reloadSections:withRowAnimation: . 这将仅重新加载指定的单元格。 如果遇到性能问题,并且每次tableView滚动到它们时都不想加载静态单元格,则可以在创建属性后将它们存储在属性中,并仅返回它们。 看起来有点像这样 ...