c++如何将以赋值的字符串写入到文件中?

people_add.name等几个字符串已经赋值了.

设计:
1. 单行存放。student.txt中每行存放一个学生的各项信息,即以换行符为区别各条学生信息。读取的时候从文件中每次提取一行来做属性判断,并筛选;
2. tab隔属性。当然也可以以其他符号,但一般来说,在学生信息内容中,一般不会使用table符号,则可以用table符号来在每行学生信息中分隔每项属性(学号、姓名等),这

样可以通过跃进一个或多个table符号,找到关心的属性,并进行判断(住址头4个字节是否是“广州”、性别属性是否为“女”等)。同时也可以在输出时选择输出部分属性了。

关键代码:
//1. read a single line from a file
char* getFileLine(FILE *argInputFile, char *argLine){
int c;
... ...
for(int i = 0; (c = getc(argInputFile)) != EOF && c != '\n'; i++){
argLine[i] = c;
}
argLine[i] = '\0';
... ...
return argLine;
}

//2. read a single property from the information line
/**
* @param *argStuInfo: some student's information which was read by function 'getFileLine()'
* @param argPropertyNo: the position of the property to be read. e.g.:
* if the properties's order is like this: StuNo. (\t)Name(\t)Gender(\t)Addr(\t)others
* in the student info, and you want to read the gender of this student, then
* you should call this function like this: getProperty(stuInfoLine, 3, gender);
*/
char* getProperty(const char *argStuInfo, const int argPropertyNo, char *argProperty){
... ...
count = 1;
for(int i = 0; (c = getc(argInputFile)) != '\0' && count < argPropertyNo; i++){
if(c == '\t'){
count++;
}
}
for(int i = 0; (c = getc(argInputFile)) != '\0' && c != '\t'; i++){
argProperty[i] = c;
}
argProperty[i] = '\0';
return argProperty;
}

没检查和编译过,差不多就是这样了。有了这些函数就可以判断类似住址头4个字节是否是“广州”、性别属性是否为“女”等这些条件了。

ps:c中对文件的操作:
stdio.h头文件中定义了对文件的引用类型:FILE 及其基本操作:
#include <stdio.h>
...
FILE *filePointer
filePointer = fopen("c://student.txt", "rw");

int ch = getc(filePointer);

putc('a', filePointer);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-30
fstream outfile("file.txt", ios::out | ios::app);
outfile << i;
第2个回答  2014-06-30
你的people_add 对象的成员都是 string类型吗?追问

对 其中我的people_add.phone_num是int,只有它可以正常使用

追答

man一下,看一下重载版本

本回答被提问者采纳
相似回答