c++从文件中读取一行数据并保存在数组中

摘要:
4#包括<67intmain()8{9ifstreaminfile;“error”<“save to vector”>23vector<ve;ve[i]<”save to array“<36inta[3][4];37int*p=&a[0][0];>42}43infile。close();对于(inti=0;i<j<a[i][j]<<

从txt文本中读取数据存入数组中

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <vector>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     ifstream infile;
10     infile.open("1.txt");
11     if(!infile) cout<<"error"<<endl;
12 
13     string str;
14     int t1;
15 /*  while(getline(infile,str))   //按行读取,遇到换行符结束
16     {   
17         infile.getline(str,10);
18         cout<<str<<endl;
19     }
20 */
21  /*//存入vector
22     cout<<"存入vector"<<endl;
23     vector<int> ve;
24     while(infile>>t1)             //按空格读取,遇到空白符结束
25     {
26         ve.push_back(t1);         
27     }
28 
29     for(int i=0;i<ve.size();i++) 
30         cout<<ve[i]<<" ";
31     cout<<endl;
32 */
33 
34 //存入数组
35     cout<<"存入数组"<<endl;
36     int a[3][4];
37     int*p=&a[0][0];
38     while(infile>>t1)             //遇到空白符结束
39     {
40         *p=t1;
41          p++;
42     }
43     infile.close();
44     for(int i=0;i<3;i++)
45     {   for(int j=0;j<4;j++)
46            cout<<a[i][j]<<"	";
47          cout<<endl;
48     }
49     return 0;
50 }

 参考博文:https://www.cnblogs.com/helinsen/archive/2012/07/26/2609251.html

http://blog.csdn.net/u010220351/article/details/52347631

免责声明:文章转载自《c++从文件中读取一行数据并保存在数组中》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇比NotePad++更好的文本代码(C#)编辑器Sublime TextJS编码方式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

时间戳(Unix时间)

/// <summary> /// 时间戳与DateTime互转 /// </summary> public class UnixOfTimeHelper { /// <summary> /// 时间转unix时间戳 /// &...

【Android】XML文件的解析

1.首先我们可以在res包路径下创建一个raw包,然后在raw下创建一个email.xml 文件,并修改其内容如下: 1 <?xml version="1.0" encoding="utf-8"?> 2 <email> 3 <from>123@163.com</from> 4 <to&g...

C# 如何获取Url的host以及是否是http

参考资料:https://sites.google.com/site/netcorenote/asp-net-core/get-scheme-url-host Example there's an given url: http://localhost:4800/account/login 获取整个url地址: 在页面(cstml)中  Microsoft...

tcl/tk之流程控制命令

作为学习笔记,写下来仅供自己查阅。 tcl脚本命令风格有些类似linux命令,比较古老和原始的语言,学过linux的看起来应该毫无压力 if 命令 if {$x < 0} {     set x 0 }   if {$x < 0} {     ... } elseif {$x == 0} {     ... } elseif {$x == 1}...

【java】Freemarker 动态生成word(带图片表格)

1、添加freemarker.jar 到java项目。 2、新建word文档。 3、将文档另存为xml 格式。 4、将xml格式化后打开编辑(最好用notepad,有格式),找到需要替换的内容,将内容换为变量(${变量名})。 5、生成表格,包括动态列和动态行。其中columnList 是List<String>格式的表头数据,datas...

asp.net父窗口、子窗口

一:转向子窗口的几种方法 1.在数据控件中(如:GridView,Repeater) 1<a href="#" onclick="window.open('money.aspx?lowhouseid=<%# DataBinder.Eval(Container.DataItem, "lowhouseid") %>&difficu...