473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

copy the content of a file into a structure array

hi everyone
i am trying to write a program for a library system that allow stuff to add, remove, view and delete customer. i try to use file to store data and i open the file in mode read then i store then i put the content of the file into a struture. now the problem started it is only showing me haft of the content and here is the coding. please help me

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. # include <windows.h>
  3. # include <stdlib.h>
  4. #include<string.h>
  5. #include<conio.h>
  6.  
  7.  
  8. void search(int s,struct books eli[20]);
  9. void view(int x,struct books eli[20]);
  10.  
  11.  
  12. struct books{
  13. char name[22], book[25],date1[10], date2[10],id[7],bid[7];
  14.  
  15. };
  16.  
  17.  
  18. int main()
  19. {
  20.  
  21.     struct books eli[20];
  22.     int num,ctr=0,index=0;
  23.  
  24.     FILE *fp;
  25.     fp = fopen("database.txt","r");
  26.  
  27.   while(fscanf(fp,"%s %s %s %s %s %s", eli[ctr].name,eli[ctr].id,eli[ctr].book,eli[ctr].bid,eli[ctr].date1,eli[ctr].date2)!=EOF)
  28.     {
  29.         fscanf(fp,"%s %s %s %s %s %s", eli[ctr].name,eli[ctr].id,eli[ctr].book,eli[ctr].bid,eli[ctr].date1,eli[ctr].date2);
  30.  
  31.     ctr=ctr+1;
  32.      index=ctr+index;
  33.  
  34.     }
  35.         fclose(fp);
  36.  
  37.  
  38.   do
  39.      {
  40.        system("cls");  
  41.          printf("\n\t\t***************************************");
  42.          printf("\n\t\t\t\t\t\t\t\t\t\t\t\t\t Welcome To BOOK MENU ");
  43.          printf("\n\t\t***************************************\n");
  44.          printf("\t\t1. Loan Transaction");
  45.          printf("\n\t\t2. Update");
  46.          printf("\n\t\t3. View Books");
  47.          printf("\n\t\t4. Exit");
  48.          printf("\n\t\t***************************************");
  49.          printf("\n\t\t Choice:");
  50.          scanf("%d", &num);
  51.  
  52.          switch(num)
  53.          {
  54.  
  55.          case 1:
  56.              search(index,eli);
  57.              break;
  58.              case 2:
  59.              view(index,eli);
  60.  
  61.                 break;
  62.              case 3:
  63.                   return 0;    
  64.                  break;
  65.              default :
  66.                  printf(" \n\t\t wrong value !!!");
  67.                  break;
  68.          }
  69.  
  70.  
  71.    }while(num!=3);
  72.  
  73.  
  74.          Sleep(2000);
  75.     }
  76.  
  77. void view(int x,struct books eli[20])
  78. {
  79.     printf("%d",x);
  80.     int ct;
  81. for(ct=0;ct<x;ct++)
  82.     {
  83.         printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",eli[ct].name,eli[ct].id,eli[ct].book,eli[ct].bid,eli[ct].date1,eli[ct].date2);
  84. }
  85. Sleep(5000);
  86. }
  87. void search(int s,struct books eli[20])
  88. {
  89.  
  90.     char name[20];
  91.     int ct=0;
  92.     printf("\n\t\t Username: ");
  93.     scanf("%s",name);
  94.     fflush(stdin);
  95.     for(ct=0;ct<s;ct++)
  96.     {
  97.     if(strcmp(name,eli[ct].name)==0)
  98.     {
  99.  
  100.     printf("\n%-13s %-13s %-13s %-13s %-13s %-13s",eli[ct].name,eli[ct].id,eli[ct].book,eli[ct].bid,eli[ct].date1,eli[ct].date2);
  101.     break;
  102.     }else
  103.         printf("could not found");
  104.  
  105.     }
  106. Sleep(2000);
  107. }
Feb 8 '14 #1
1 5304
weaknessforcats
9,208 Expert Mod 8TB
I have no idea how your text file was created but I can tell you that the trick in dealing with structs and files is knowing exactly how the text file was created.

You are using fscanf and %s which means a string with a null terminator. How did the null terminator get into the text file in the first place?

If you are creating the text file using a text editor, then your reading of the file has to match what you typed. For example, the name can be 22 char. That means your read function must read 22 bytes OR it reads fewer bytes because you have placed a delimiter in the text file, like a comma, to stop the read before 22 characters. The read function can copy into the struct name.

Expand|Select|Wrap|Line Numbers
  1. weaknessforcats,How to Code,2014-02-01,etc...
Here you read until you reach a comma. Since the struct has 6 members you need 5 commas before the newline character. You need to know which comma you read so you can copy to yhe correct struct member.

I recommend two functions when dealing with structs and files. First, a PACK that takes a struct and builds ne string out of the struct members and inserts those commas. Second, and UNPACK which takes a single string and parses it into a struct:

Expand|Select|Wrap|Line Numbers
  1. void pack(struct books* book, char** string);
  2. void unpack(char** string, struct books* book);
You pack, then write or you read and then unpack. Often I combine the pack and the write into ne function and the read an unpack into another function. This way all the logic of dealing with the file is in one spot. The removes a lot of clutter from main().

Let me know what you think.
Feb 8 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bob Bedford | last post by:
I've created a function with takes some files (input type="file") as parameter. Now, since this is an array of a structure, I'd like to fill this structure with my own datas, as I'd like to use...
1
by: deko | last post by:
DoCmd.CopyObject copies data, but I only need structure. I'm trying to clone several tables in my Access 2003 mdb. The goal is to link to a series of Excel spreadsheets and then run various...
8
by: david | last post by:
I have made a test: copy a text file to clinet side. But fails. What I did is that create a web method in web services that read a text in server side and return the text file as string. On the...
5
by: s88 | last post by:
Howdy: the follows is my program, I wanna change my structure array pointer in the function "testfunc", but I fail..., I also try to call the testfunc by reference, but the compiler says...
3
by: ShuchiTandon | last post by:
Hi all, i m trying to read from a file and then copy it into an array...my code is as follow..it runs fine but i cant understand y it doesnt show me any output?? here is my code... using...
0
by: JoeyB | last post by:
Hey All, My app will have a structure array that will hold the parameters used to initialize a array of classes at run time to default values. XML looks like a good candidate to persist the data...
13
by: sachin | last post by:
Hi, Is it possible to do something like this: unsigned char arr = { #include "cFile.c" } I need that C source file cFile.c to compile and its binary output to include in array.
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
7
by: Arthur | last post by:
hi, this might be a simple one to them who know: i'm developing an asp.net project and use the debug mode. i have a xml file which contains data, that shall be read, if a specific page is...
7
abdoelmasry
by: abdoelmasry | last post by:
Hi Pros I need help to read xml to array, xml file structure is unknown, and has many elements and subelements, so it should be imported to multiple array. here is xml file as example:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.