[英]C read txt file, save information in struct and call with a function
[英]Read file .txt and save it to struct in c
我想问一下C语言中的文件处理和结构,我从演讲中得到了一个作业,并对C编程中的字符串操作感到非常困惑。 这是任务。
这是mhs.txt
1701289436#ANDI#1982
1701317124#WILSON#1972
1701331734#CHRISTOPHER STANLEY#1963
1701331652#SHINVANNI THEODORE#1962
1701331141#MUHAMMAD IMDAAD ZAKARIA#1953
1701331564#MARCELLO GENESIS DRIET J.#1942
1701322282#ANANDA AULIA#1972
1701329175#LORIS TUJIBA SOEJONOPOETRO#1983
1701301422#DEWI JULITA#1993
1701332610#HARRY HUTALIANG#1982
#之前的第一个是NIM,第一个#是名称之后,#之后的最后一个是年份
这就是我所做的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student{
char nim[11];
char name[50];
int year;
}s[10];
int main(){
FILE *fp;
int c,i,n;
printf("Read mhs.txt...");
getchar();
fp = fopen("mhs.txt", "r");
c = getc(fp);
i = 0;
while(c!=EOF){
printf("%c", c);
c = getc(fp);
i++;
}
fclose(fp);
getchar();
return 0;
}
首先,我可以将数据保存在struct上,但是在这里我非常困惑以分隔一个字符串。
这就是我对结构和文件处理的全部了解,有人可以帮助我吗? 我已经上网了,找不到正确的结果。
对不起,如果有重复的问题,对不起,如果我的英语不好。
由于您已将其标记为C ++,因此我将使用C ++:
#include <iostream>
#include <string>
#include <algorithm>
struct student {
std::string nim;
std::string name;
int year;
bool operator<(student const &other) {
return name < other.name;
}
friend std::istream &operator>>(std::istream &is, student &s) {
std::getline(is, s.nim, '#');
std::getline(is, s.name, '#');
return is >> s.year;
}
};
int main() {
std::ifstream in("mhs.txt");
std::vector<student> students{
std::istream_iterator<student>(in),
std::istream_iterator<student>()
};
std::sort(students.begin(), students.end());
}
如果您想在C语言中完成大致相同的任务,则最简单的方法是使用scanset转换使用fscanf
进行读取,例如:
fscanf(infile, "%10[^#]#%49[^#]#%d", student.nim, student.name, &student.year);
scanset转换为您提供了正则表达式的子集,因此%[^#]
会将一串字符转换为(但不包括) #
。 在这种情况下,我将每个数组的长度限制为比您在结构定义中为数组指定的长度小一,以防止缓冲区溢出。
然后,您可以使用qsort
进行排序。 您将需要编写一个比较函数,但是正确地进行操作并不总是很明显:
int cmp(void const *aa, void const *bb) {
student const *a = aa;
student const *b = bb;
return strcmp(a->name, b->name);
}
这里有一些提示,而不是完整的答案。 希望它能对您有所帮助。
首先,您需要逐行读取文件,而不是逐字符读取文件。 您需要fgets()
函数。 您可以从www.cplusplus.com/reference/cstdio/fgets/找到该参考。
第二,您可以使用strtok()
分隔字符串。 这是一个例子。
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
并且您可以从http://www.cplusplus.com/reference/cstring/strtok/找到对strtok()
的引用
第三,使用qsort()
对结构数组进行排序。 您可以从http://www.cplusplus.com/reference/cstdlib/qsort/找到它的参考。 例子也可以在那里找到。
这是纯C代码,您应该新增三个导入功能: strtok
& qsort
& fsan
。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char nim[11];
char name[50];
int year;
};
#define BUFFER_SIZE 100
struct student saveToStruct (char* str)
{
struct student res;
int flag = 0;
char *token = strtok(str, "#");
while( token != NULL )
{
if (0 == flag)
strcpy(res.nim, token);
else if (1 == flag)
strcpy(res.name, token);
else
res.year = atoi(token);
flag++;
token = strtok( NULL, "#" );
}
return res;
}
void print(struct student* arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%s, %s, %d\n", arr[i].nim, arr[i].name, arr[i].year);
}
}
int cmp(const void* l, const void* r)
{
return strcmp(((const student*)l)->name, ((const student*)r)->name);
}
int main()
{
struct student arr[10];
FILE* file = fopen("mhs.txt", "r");
if (!file)
return -1;
char buffer[BUFFER_SIZE];
int flag = 0;
while (fgets(buffer, BUFFER_SIZE, file))
{
arr[flag] = saveToStruct(buffer);
flag++;
}
print(arr, 10);
qsort(arr, 10, sizeof(struct student), cmp);
printf("After sort by name!\n");
print(arr, 10);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.