2012-11-24 95 views
0

我不知道为什么第一个fwrite()customer工作,但不是第二个fwrite()为消息的符号。我的程序要检查客户和消息中的符号是否已包含在库.dat中。这里是我的一段代码:第一个fwrite工作但不是第二个

typedef struct { 
    char Name[50]; 
    int totalmsg; 
    int totalword; 
} Customer; 

typedef struct { 
    char symbol; 
    char alphabet[20]; 
} Ssymbol; 

void add_customer() { 
    boolean found; 
    int i; 

    fp = fopen("customer.dat", "wb+"); 
    fread(&Customer_history, sizeof(TabInt), 102, fp); 

    i = GetLastIdx(Customer_history); 
    do { 
     printf ("Please input customer's name: ");scanf("%s",&temp); 
     if (i == 0) { 
      i++; 
      SetName(&Customer_history,i,temp); 
      SetMsg(&Customer_history,i,i); 
      SetEff(&Customer_history,i); 
      printf("Do you still wanna add another customer ?(Y/N)"); scanf("%s",&CC); 
     } 
     else { 
      found = Search_name(Customer_history,temp); 
      if (found == true) { 
       printf("The name is already exist \n\n"); 
      } 
      else { 
       i++; 
       SetName(&Customer_history,i,temp); 
        SetMsg(&Customer_history,i,i); 
        SetEff(&Customer_history,i); 
      } 
      printf("Do you still wanna add another customer ?(Y/N)"); scanf("%s",&CC); 
     } 
    } while ((CC == 'y') || (CC =='Y')); 
    fwrite(&Customer_history, sizeof(Customer), 102, fp); 
    fclose(fp); 
} 

void add_symbol() { 

    char tempc; 
    char tempalphabet[20]; 
    boolean found; 
    int i; 

    fp = fopen("library.dat","wb+"); 

    fread(&list_symbol, sizeof(Ssymbol), 52, fp); 

    i = GetLastIdx2(list_symbol); 

    do{ 
     printf("Please input new symbol:"); 
     scanf("%s", &tempc); 

     printf("Please input the alphabet of the symbol:"); 
     scanf("%s", &tempalfabet); 

     if (i==0){ 
      i++; 
      SetSymbol(&list_symbol,i,tempc); 
      SetAlphabet(&list_symbol,i,tempalphabet); 
      printf("Do you want to add another symbol? (Y/N)"); 
      scanf("%s",&CC); 
     }  
     else{ 
      found = Search_symbol(list_symbol, tempc); 
      if (found==true){ 
       printf("Symbol is already exist \n\n"); 
      } 
      else{ 
       i++; 
       SetSymbol(&list_symbol,i,tempc); 
       SetAlphabet(&list_symbol,i,tempalphabet); 
       printf("Do you want to add another symbol? (Y/N)"); 
       scanf("%s",&CC); 
      }    
     } 
    } 

    while((CC=='y') || (CC=='Y')); 

    fwrite(&list_symbol, sizeof(Ssymbol),52, fp); 
    fclose(fp); 
} 
+0

错误代码。 Check'em。 –

+0

什么是Customer_history和list_symbol声明?除非有错误代码,否则fwrite不能忽略它的首选项,它只是字节。 – keltar

+0

疯狂猜测,fread会更改文件偏移量,因此您在第一个记录之后写入第二个记录。 'fwrite'之前的'fseek(fp,0,SEEK_SET)'应该解决这个问题。 – Petesh

回答

0

我想猜测这个问题,因为整个代码不是vailable ..

难道符号的数量超过52,和你只写了第52个符号?所以fwrite确实有效,但它不会改变数据文件。尝试printf'ing I(= GetLastIdx2),以查看其是否是超过52

另外,代替具有用于list_symbols阵列,我建议使用链表&的fread/fwrite的,使用一个while循环,以读取/写入数据文件中的所有符号。

0

从代码逻辑,它想要从文件中读取一些数据,然后更新它。

然后你应该使用“rb +”模式打开文件,而不是“wb +”,它会截断整个数据。在“wb +”模式下,文件数据被截断,并且fread()应该返回0,整个do/while变得无效。

我不知道为什么第一个fwrite()工程,他们都不应该。

总之,尝试ab+模式fopen(),不要忘记调用fseek()与SEEK_SET调用fwrite()之前重置文件指针位置,它可能会解决你的问题。

4

如果只期待一个单个字符的位置:

scanf("%s", &tempc); 

然后将其更改为:

scanf("%c", &tempc); 

否则,您需要定义tempc为数组(不要使用& )。

这里也是,因为土特产品都传递一个数组,你不应该使用地址的(&):

printf("Please input the alphabet of the symbol:"); 
// scanf("%s", &tempalfabet); is wrong 
scanf("%s", tempalfabet); // correct 

检查代码中的所有scanf函数的调用和解决这些问题

2

这很难说没有你的代码更“编译能”的版本......我这里还有一些给你点:

A)boolean不是C的类型,你会需要这样的东西:

#include <stdbool.h> // or some other header that defines bool 
typedef bool boolean; 

做不到这一点:

typedef int boolean; 
#define false 0 
#define true 1 

B)add_customer()和使用未限定fpadd_symbol()。您需要为这些操作定义一个FILE * fp

C)下述(i假设为)的方法并不限定:GetLastIdx()SetName()SetMsg()SetEff()Search_Name()GetLastIdx2()SetSymbol()SetAlphabet()Search_symbol()

d)以下(I假定为)全局变量和数据structers没有定义:Customer_historyTabInttempCClist_symbol,根据您的问题说明我tempalfabet

假设你有这个工作,所以这些都应该被定义。如果您可以提供更完整,可编译的代码版本来显示问题,我可以提供更多帮助。

因此,基于我有什么,这里的一些输入:

1)你正在阅读到您的list_symbol,这应该是一个指向的内存为52*sizeof(Ssymbol)足够大的块,这应该是各地的1092个字节。

fread(&list_symbol, sizeof(Ssymbol), 52, fp); 

还要注意,如果list_symbolSsymbol当时的你读代码的事实,你有你的结构数组被搞砸了一个数组,fread()将读取并填写您的资料为字节正好字节。例如:

Ssymbol list_symbol[52]; 
FILE * fp = fopen("test.txt", "r"); 
memset(list_symbol, 0, 52*sizeof(Ssymbol)); 
fread(list_symbol, sizeof(Ssymbol), 52, fp); 

而且,我们说 “的test.txt” 中有:

C abcdefghijklmn 
3 12345678 

然后你list_symbol看起来像:

list_symbol[0].symbol = 'C' 
list_symbol[0].alphabet = ' abcdefghijklmn3 12' //note the newline and spaces fill it 
list_symbol[1].symbol = '3' 
list_symbol[1].alphabet = '45678' 

这可能不是你想做。确保你的数据与你的结构相匹配!

2)你的一个scanf()您完全显示是错误的:

char tempc; 
... 
scanf("%s", &tempc); 

你想要的 “%C” 如果你想要一个字符,而不是 “%S”。我没有看到上面的其他声明(参见D点),但是根据您的印刷品和您的使用情况,我可以告诉您tempalfabetCC几乎肯定是错误的。

3)你scanf() s的打算,除非你消耗他们离开换行符:

printf("Please input new symbol:"); 
scanf("%c\\n", &tempc); // <-- escaped newline will do that for you 

4)你用 “WB +” 打开文件。请参阅fopen() options这是写入和追加到二进制文件。如果你想要一个二进制文件到读写你需要:“ab +”,如果你想要一个文本文件“a +”就可以。

5)你不要重置文件指针:

fopen(file for reading, writing, and appending); 
fread(stuff); 
do c code; 
fwrite(stuff); 

这将导致你所写的内容被附加到文件的底部。 我不知道这是你想要的。因此,如果您想要读取完整的list_symbol数据,那么这可能不会成为问题,然后在文件的底部添加一组新的数据。我嫌疑人你想覆盖你的文件中的内容然而...在这种情况下,你需要fseek()回到开头,或者你可以简单地关闭并打开文件。

相关问题