【问题标题】:I'm unable to write the structure in a text file in C我无法在 C 中的文本文件中写入结构
【发布时间】:2016-01-21 19:00:38
【问题描述】:

我为一个学校项目制作了这个程序。它运行得很好,只是我无法根据需要将数据写入文本文件。我得到的文件文本如下:

AJames y$¥ °› †løvúövÿÿÿÿ$ „þB-28 ®kt¼› €@ ° @ ¬þ ...tt¸?'tìþ¢it Mark $¥€@ €@ ° @ Àþ øþÌÿ IT tÕÑþÿÿÿ áD

我的 C 语言不强,因此,即使浏览了在线论坛,我也无法找到具体的答案。以下是代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<string.h>

int main()
{
  FILE *fp, *ft;
  char tmp, choice;

  struct fee
 {     float r_no;
       char name[50];
       char add[50];
       char f_name[40];
       char stream[10];
       float d_fee;
  }f;
long int amount;
int flag;
long int total = 2000;
long int pending=0;
long int size;
char stream1[40];
float r_no1;

     fp=fopen("FEES.txt","w");
     if(fp==NULL)
     {
            puts("Cannot open file");
            exit(1);
     }

size = sizeof(f);
while(1)
{
     system("cls");

     printf("\n\t\tWelcome to Main Menu!!");

     printf("\n\t\t1. Student Detail");

     printf("\n\t\t2. Display Record");

     printf("\n\t\t3. Deposit Fee");

     printf("\n\t\t4. Pending Fee");

     printf("\n\t\t5. Delete Record");

     printf("\n\t\t0. Exit");

     printf("\n\n\tYour choice : ");
     fflush(stdin);
     choice = getche();
     switch(choice)
     {
            case '1':
            fseek(fp,10,SEEK_SET);
            tmp = 'Y';
            while((tmp == 'Y') || (tmp == 'y'))
            {
                        printf("\n\tEnter Roll No: ");
                        scanf("%f", &f.r_no);
                        printf("\n\tEnter Name: ");
                        scanf(" %s", f.name);
                        printf("\n\tEnter Address: ");
                        scanf(" %s", f.add);
                        printf("\n\tEnter Father Name: ");
                        scanf(" %s", f.f_name);
                        printf("\n\tEnter Deposited Fee: ");
                        scanf("%f", &f.d_fee);
                        printf("\n\tEnter Stream: ");
                        scanf(" %s", f.stream);
                        fwrite(&f, size, 1, fp);
                        printf("\nCreate Another Student Record(Y/N)");
                        tmp = getche();
            }
            break;

            case '2':
            system("cls");
            rewind(fp);
            printf("Roll No: \tName: \tAddress: \tFather's Name: \tStream: \tDeposited Fee: \tTotal Amount: ");
            while(fread(&f, size, 1, fp))
            printf("\n%d \t%s \t%s \t%s \t%s \t%f \t%ld", f.r_no, f.name, f.add, f.f_name, f.stream, f.d_fee, total);
            getch();
            break;

            case '3':
            tmp='Y';
            while(tmp=='Y' || tmp=='y')
            {
                        printf("\nEnter Roll Number to Check Balance");
                        scanf("%d", &r_no1);
                        printf("\nEnter the Stream");
                        scanf("%s", &stream1);
                        rewind(fp);
                        flag=0;
                        while(fread(&f, size, 1, fp))
                        {
                                    if((f.r_no==r_no1)&&(strcmp(f.stream,stream1)))
                                    {
                                        if(f.d_fee<=total)
                                        {
                                                flag=1;
                                                printf("\nEnter amount to Deposit");
                                                scanf("%ld", &amount);
                                                f.d_fee = f.d_fee + amount;
                                                fseek(fp, -size, SEEK_CUR);
                                                fwrite(&f, size, 1, fp);
                                                break;
                                        }
                                    }
                        }
                                    if(flag==0)
                                    {
                                            printf("Sorry! Invalid Roll No.");
                                            getch();
                                    }
                        printf("Deposit More Amount!");
                        fflush(stdin);
                        tmp=getche();
                }
                break;
            case '4':
            tmp='Y';
            while(tmp=='Y'||tmp=='y')
            {
                        printf("\nEnter Roll Number to check Pending Amount");
                        scanf("%d", &r_no1);
                        printf("\nEnter the Stream");
                        scanf("%s", &stream1);
                        rewind(fp);
                        flag=0;
                        while(fread(&f, size, 1, fp))
                        {

                                    if((f.r_no==r_no1)&&(strcmp(f.stream,stream1)))
                                    {flag=1;
                                                pending=total-f.d_fee;
                                                printf("\nYour pending amount is: %ld", pending);

                                                getch();
                                    }
                        }
                        if(flag==0)
                        {
                                    printf("Sorry! Invalid Roll Number");
                                    getch();
                        }

                        fflush(stdin);
                        tmp=getche();
            }
            break;

            case '5':
            tmp='Y';
            while(tmp=='Y'||tmp=='y')
            {
                        printf("\nEnter Roll Number to delete");
                        scanf("%d",&r_no1);
                        ft = fopen("TEMP.txt","w");
                        rewind(fp);
                        while(fread(&f, size, 1, fp))
                        {
                                    if(f.r_no!=r_no1)
                                    fwrite(&f, 1, size, ft);
                        }
                        fclose(fp);
                        fclose(ft);
                        remove("FEES.txt");
                        rename("TEMP.txt", "FEES.txt");
                        fp=fopen("FEES.txt", "r");
                        printf("Delete another record(Y/N)");
                        fflush(stdin);
                        tmp = getche();
            }
            break;

            case '0':
            fclose(fp);
            printf("\n\t\tProgram Terminated!!");
            exit(0);
     }
}}

好的,正如建议的那样,我使用 fprintf() 而不是 fwrite(),现在我在我的程序上获得了这种力量。这是我所做的更改: 案例“1”:

        tmp = 'Y';
        while((tmp == 'Y') || (tmp == 'y'))
        {
                    printf("\n\tEnter Roll No: ");
                    scanf("%f", &f.r_no);
                    printf("\n\tEnter Name: ");
                    scanf(" %s", f.name);
                    printf("\n\tEnter Address: ");
                    scanf(" %s", f.add);
                    printf("\n\tEnter Father Name: ");
                    scanf(" %s", f.f_name);
                    printf("\n\tEnter Deposited Fee: ");
                    scanf("%f", &f.d_fee);
                    printf("\n\tEnter Stream: ");
                    scanf(" %s", f.stream);
                    fprintf(fp, "%d %s %s %s %f %s \n", f.r_no, f.name, f.add, f.f_name, f.d_fee, f.stream);
                    printf("\nCreate Another Student Record(Y/N)");
                    tmp = getche();

任何帮助将不胜感激!

【问题讨论】:

  • 这个fflush(stdin); 是未定义的行为。
  • 使用fprintf 而不是fwrite
  • 将结构写入文件不会将其转换为文本,它只是写入结构的原始字节。您需要使用fprintf()根据需要格式化每个字段。
  • 哦,还有。您的代码非常混乱且难以阅读。您可能希望改进您​​的编码风格,以使您的程序更易于调试。
  • 我会为每个选项制作一个单独的过程,以使代码更易于阅读。

标签: c structure text-files codeblocks file-handling


【解决方案1】:

你写的不是 text 文件而是二进制文件,你不能用纯文本编辑器检查它的内容。您可以使用 HEX 编辑器,其中一些甚至可以接受定义一个结构来逐条验证文件记录(如果需要)。

或者您需要编写一个读取函数,将二进制数据直接读取到您的结构实例中。然后根据它的类型打印结构的每个成员。

请注意,"w" 模式虽然适用于文本文件,但您应该改用 "wb"

【讨论】:

  • 我认为他想写文本,而不是二进制。
  • 也许,但我认为这解释了为什么使用文本编辑器检查文件会得到结果。
  • 是的,我想写一个文本文件而不是二进制文件,如果你能详细说明如何将信息正确写入文本文件。
  • 谢谢你..!我试试看!
  • 您还希望将每条记录保持固定大小,以便您可以轻松地随机访问它(即开始或记录位置 = 记录编号 * 某个固定值)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多