473,408 Members | 1,749 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,408 software developers and data experts.

writing to file

56
i am trying to sort an array of structures and put it in a file
here is the code
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<string.h>
  3.  
  4. struct Contacts
  5. {
  6.     char name[20];
  7.     int phone;
  8.     char address[20];
  9.  
  10. };
  11. int countLines(FILE * fp);
  12.  
  13. void readfromfile(int c);
  14.  
  15.  
  16.  
  17. int main(int argc, char** argv) {
  18.     FILE *f=fopen("D:\\test.txt","r");
  19.     int c;
  20.     c=countLines(f);
  21.  
  22.     printf("number of lines %d  \n \n ",c);
  23.  
  24.  
  25.     readfromfile(c);
  26.  
  27.  
  28.     return 0;
  29. }
  30. int countLines(FILE * fp){
  31.     char line[80];
  32.     int counter = 0;
  33.     while ( fgets(line, sizeof line, fp) ){ 
  34.  
  35.  
  36.         counter+=1;
  37.  
  38.     }
  39.     return counter;
  40. }
  41.  
  42.  
  43.   void readfromfile(int c)
  44.   {
  45.       int i,j;
  46.  
  47.       struct Contacts array[c];
  48.       FILE *f=fopen("D:\\test.txt","rw");
  49.       struct Contacts temp;
  50.       for(i=0;i<c;i++)
  51.       {
  52.           fscanf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
  53.           printf("%s %d %s \n \n ",array[i].name,array[i].phone,array[i].address);
  54.     }
  55.  
  56.       for(i=0;i<c;i++)
  57.       {
  58.           for(j=1;j<c;j++)
  59.           {
  60.               if(strcmp(array[i].name,array[j].name) > 0 )
  61.               {
  62.                   temp=array[i];
  63.                   array[i]=array[j];
  64.                   array[j]=temp;
  65.  
  66.               }
  67.           }
  68.       }
  69.  
  70.  
  71.           fclose(f);
  72.  
  73.  
  74.     FILE* fp = fopen("D:\\test.txt", "a");
  75.     for(i=0;i<c;i++)
  76.     {
  77.     fprintf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
  78.     }
  79.  
  80.       fclose(fp);
  81.  
  82.  
  83.   }
when i open the file which is written in it, a garbage is found in the file
would anyone tell me what is the problem of this
Nov 28 '14 #1
3 1749
weaknessforcats
9,208 Expert Mod 8TB
This struct:

Expand|Select|Wrap|Line Numbers
  1. struct Contacts
  2. {
  3.     char name[20];
  4.     int phone;
  5.     char address[20];
  6.  
  7. };
when you use fprintf to write the name with a %s says to write characters until you reach a \0. If the name is FOX you would write 3 characters and not 20. That would atart the phone in position 4 instead of 21. Not good.

The name has to be written as 20 bytes regardless of the actual length of the name. The easiest thing is to use fwrite and write the sizeof(struct Contacts) for the number of elements in your array.

You would then use fread to read back into a buffer the sizeof(struct Contacts) x the number of array elements.

You can't look at this file using a text editor since the above method does write a text file.

If you need to see the file in characters using a text editor, then you must write 20 characters for the name, convert the phone to a 12 char array and write the 12 chars, and finally write 20 characters for the address.
Nov 28 '14 #2
Norgy
56
i have tried to use fread and fwrite but nothing changed

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<string.h>
  3. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  4. struct Contacts
  5. {
  6.     char name[20];
  7.     int phone;
  8.     char address[20];
  9.  
  10. };
  11. //functions prototypes
  12.  int countLines();
  13.  //void write (int c);
  14.   void sort_name(int c);
  15.  
  16.  
  17.  
  18. int main(int argc, char** argv) {
  19.  
  20.     int c;
  21.     c=countLines();
  22.  
  23.     printf("number of lines %d  \n \n ",c);
  24.        // write ( c);
  25.  
  26.    sort_name(c);
  27.  
  28.  
  29.     return 0;
  30. }
  31. int countLines(){
  32.     char line[80];
  33.     FILE *fp=fopen("D:\\test.txt","r");
  34.     int counter = 0;
  35.     while ( fgets(line, sizeof line, fp) ){ 
  36.  
  37.  
  38.         counter+=1;
  39.  
  40.     }
  41.     fclose(fp);
  42.     return counter;
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  //read from file 
  49.   void sort_name(int c)
  50.   {
  51.       int i,j;
  52.  
  53.       struct Contacts array[c];
  54.       FILE *f=fopen("D:\\test.txt","r");
  55.       struct Contacts temp;
  56.       for(i=0;i<c;i++)
  57.       {
  58.           fscanf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
  59.           fread(array, sizeof(array), 1, f);
  60.  
  61.       }
  62.  
  63.       for(i=0;i<c;i++)
  64.       {
  65.           for(j=1;j<c;j++)
  66.           {
  67.               if(strcmp(array[i].name,array[j].name) > 0 )
  68.               {
  69.                   temp=array[i];
  70.                   array[i]=array[j];
  71.                   array[j]=temp;
  72.                   printf("sssss");
  73.  
  74.               }
  75.           }
  76.       }
  77.  
  78.  
  79.  
  80.           fclose(f);
  81.  
  82.  
  83.           FILE* fp = fopen("D:\\test.txt", "w");
  84.     for(i=0;i<c;i++)
  85.     {
  86.         fwrite(array, sizeof(array), 1, fp);
  87.  
  88.     }
  89.  
  90.           fclose(fp);
  91.  
  92.  
  93.   }
Nov 29 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. fscanf(f,"%s %d %s",array[i].name,&array[i].phone,array[i].address);
  2. fread(array, sizeof(array), 1, f);
  3.  
The fscanf is altering your position in the file and screwing up the fread. Just use the fread. The data will be in element 0 of the array. I suspect you will want to read the entire file in one fread so you should read c elements into array at the beginning.

That should be the end of the fread. The rest of the code just sorts the array and never touches the file again.

Do you know how to use your debugger?
Nov 30 '14 #4

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

Similar topics

0
by: JC | last post by:
I am using Mysql version: 4.0.18-max-log and trying to populate a database from a text file using source option from inside the mysql environment. The population run OK but when creating the...
2
by: CJ | last post by:
I've got following: > file_a content needs to be compared with file_b content. I want to write content in binary form, then run a line by line comparison on characters. When the first different...
1
by: brabster | last post by:
Hi, this is my first post so please excuse any mistakes! I'd like to write files using an upload script in my website, but I'm having problems. I don't seem to be able to write a file. Ive set up...
1
by: pablo.erba | last post by:
Hi Everyone, I´m new to this group, I´m trying to write a text files adding content from a text column (more than 8000 characters), I found code how to write files and it works but i have the...
6
by: William Manley | last post by:
According to what I read in the ConfigParser module, this should work, but it doesn't. Can anyone point out what I'm doing wrong? def writeinifile ( self, FILE='P:/INI/test.ini',...
1
by: Debabrata Jana | last post by:
Hi all, I use solaris 5.8 OS. I have three OS user : 1) orac9ias 2)oracle9i 3)backup I use the following command to export a oracle export backup (I use oracle9i release 1):
4
by: FingerDemon | last post by:
I'm guessing I'm missing something obvious here, but I have searched around and re-read my Python books and references on simple file writing and I can't see the answer. I am running this in Windows...
1
by: Mr fahad | last post by:
#include <stdio.h> #include <conio.h> void main (void) { char ch; FILE *fp; fp=fopen ("fahd.txt","w"); while (ch=fgetc( )!='\r') fputc (ch,fp); fclose (fp);
4
by: Jim | last post by:
Hi There, I'm trying to read a file character by character. When I write the file out, there is one extra character which shows on the screen as a solid circle with a small question mark in the...
1
by: robertcullen | last post by:
when we finish writing a file...How and when EOF gets associated with it..?? if EOF is only -1 (and nothing else) What will happen while reading another -1 (which is within the file)?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.