简体   繁体   English

使用文件结构将数据从一个程序移动到另一程序

[英]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: 我不会为您编写完整的代码,但是这对您的程序应该做什么有一些帮助:

  1. read in the file line by line. 逐行读取文件。 (functions fscanf , fgets or getline may be useful) (函数fscanffgetsgetline可能有用)
  2. extract the name and grade out of the line. 从行中提取名称和等级。 ( sscanf and all string functions) sscanf和所有字符串函数)
  3. check if the name is the one you are looking for, if yes print it and stop. 检查名称是否是您要查找的名称,如果是,则将其打印并停止。

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.

相关问题 是否可以在一个 C 程序中“创建”垃圾 memory 数据,然后再从另一个 C 程序中读取相同的数据? - Is it possible to "create" garbage memory data in one C program, and then later read in that same data in from another C program? C程序:如何从一个文件读取并写入另一个文件? 索引编制 - C program: How to read from one file and write into another? Indexing c程序将媒体文件从一个位置复制到另一个位置 - c program to copy media file from one location to another 使用C将数据从一个程序发送到另一个非阻塞 - Sending data from one program to another non-blocking in C 使用系统 gcc 命令从另一个程序运行 c 程序 - Run c program from another one using system gcc command 在C程序中为通讯录设计数据结构? - Designing data structures for an address book in C program? 使用一个程序的局部变量作为另一个 C 程序的全局变量 - using local variable of one program as global variable of another c program C-将输入从一个程序传递到另一程序 - C - Pipe input from one program to another program 使用结构计算 sgpa 的 C 程序 - C Program to calculate sgpa using structures 我制作了从一个文件复制数据并使用(读、写)粘贴到另一个文件的程序,但我认为它花费了太长时间 - I made program that copies data from one file and pastes to another using (read,write) but i think its taking too long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM