神奇的读入读出

A. 文件读入读出:

 1 /*文件读入读出*/
 2 
 3 #include<cstdio>
 4 #include<iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     freopen("test.in","r",stdin);
 9     freopen("test.out","w",stdout);
10     return 0;
11 }
 1 /*文件读入读出*/
 2 
 3 #include<cstdio>
 4 #include<iostream>
 5 #include<string>
 6 #include <cstdlib>
 7 using namespace std;
 8 const int N = 100;
 9 int main()
10 {
11     FILE *f = fopen("a.txt", "r");
12     FILE *f2 = fopen("b.txt", "w");
13     //FILE *f3 = fopen("c.txt","r")
14 
15 
16 
17     while(1) {//设计多个文件处理的时候好用,而且可以定向的对文件进行操作
18         int tt = rand();
19         fprintf(f2, "%d", tt);
20         printf("%d
", d);//不会影响对屏幕的输入输出
21         fclose(t);
22     }
23     
24     
25     for(int d = 1; d < 100; d++) {
26         char ts[] = "";
27         sprintf(ts, "%d.txt", d);//批量生成多个文件
28         //sprintf(ts, ".txt");
29         FILE *t = fopen(ts, "w");
30         fprintf(t, "%d", d);
31         fclose(t);
32         int tt = rand();
33         //fprintf(f2, "%d", tt);
34         printf("%d
", d);
35     }
36     return 0;
37 }

B. 字符串读入

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     string s;
 9     cin>>s;
10     cout<<s;
11     char sss[100];
12     //getline 只能是char数组,因为第一个参数是一个地址。
13     cin.getline(sss,100,'
');//100表示最大字符数,第三个参数表示读到什么字符结束,默认的是换行
14     //注意,前面要是有cin 或者是scanf("%d",d);之类的要先将前面函数的换行读掉,用getchar();
15     cout<<s;
16     //字符数组两种定义方法
17     char mp[100];
18     char ss[] = "";
19 
20     printf("%s
",s.c_str());//可以将string转化成char 数组
21     s = sss;//也可以将字符数组准换成string
22 
23     scanf("%s",mp);//和getline一样要先把之前的换行读掉。用getchar();
24     scanf("%s",ss);
25 
26     char ch;
27     getchar();
28     int a;
29     double b;
30 
31     scanf("%d %lf %c",a,b,ch);
32 
33 
34 
35     return 0;
36 }

C.gets() , get() , getline()的区别

get()每次读取一整行并把由Enter键生成的换行符留在输入队列中

getline()每次读取一整行并把由Enter键生成的换行符抛弃

gets()每次读取一行数据

http://laomaizi.blog.51cto.com/487070/105281/

D. 二进制读入读出必须使用read() 和write() 方法读取和写入二进制文件 这个还不是很了解。。。。

原文地址:https://www.cnblogs.com/shanyr/p/5537188.html