[英]Moving data from one program to another program using file structures c
Program(A)-----> file.txt-----> Program(B) 程序(A)-----> file.txt ----->程序(B)
^This is the format I am using, I currently don't have enough knowledge with file structures. ^这是我使用的格式,我目前对文件结构了解不足。 My text file is named myStudents.txt
我的文本文件名为myStudents.txt
EDIT: Program(A) writes the information properly. 编辑:程序(A)正确写入信息。 Program(B) needs to retrieve the information from the text file. 程序(B)需要从文本文件中检索信息。
#include<stdio.h>
int main()
{
char studentName[50];
int grade=0;
printf("Which students grade would you like to retrieve?: ");
scanf("%s",&studentName);
FILE *fptr;
fptr = (fopen("myStudents.txt", "r"));
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("\nStudent details:\n");
fscanf(fptr,"%d %[^\n]s",grade,studentName);
printf("Name: %s\n",studentName);
printf("Grade: %d\n",grade);
fclose(fptr);
return 0;
}
I'm very confused on how to use program A's information in program B. Apologies if this is a repeat thread, I couldn't find any information here or anywhere else to solve my issue. 我对如何在程序B中使用程序A的信息感到非常困惑。抱歉,如果这是重复线程,我在这里或其他任何地方都找不到任何信息来解决我的问题。
*Note(A solid explanation would be very helpful along with any constructive criticism) *注(扎实的解释以及任何建设性的批评都将非常有帮助)
Cheers! 干杯! Have a good day! 祝你有美好的一天!
Your program B does actually not search for any name, it just tries to print the first one. 您的程序B实际上不搜索任何名称,它只是尝试打印第一个。 I won't write the complete code for you but here is a little help with what your program should do: 我不会为您编写完整的代码,但是这对您的程序应该做什么有一些帮助:
fscanf
, fgets
or getline
may be useful) (函数fscanf
, fgets
或getline
可能有用) sscanf
and all string functions) ( sscanf
和所有字符串函数) This is of course only an example what the program could look like, but i suggest to start by implementing this steps. 当然,这仅是程序外观的一个示例,但我建议从实现此步骤开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.