Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Pls Help me ,simple work but Iam in Confusion. (http://www.go4expert.com/forums/pls-help-simple-iam-confusion-t7311/)

Shilpa1 15Nov2007 16:26

Pls Help me ,simple work but Iam in Confusion.
 
Hai,
Iam new to programming.
Iam reading contacts fron PhoneBook.Using "fopen" I opened a file "Contacts.txt".Now,I want to write the contacts to File using fwrite.

In File, I want the Contacts as
Contact1
Contact2
Contact3....
i.e, I want to write the contacts line by line to File.

On other side,I want to read the contacts line by line.First,I want to read the First Contact write it to a Buffer,then read the 2nd contact write to Buffer like that.How this is possible?pls help me.

Sorry,for my poor English.

shabbir 15Nov2007 18:40

Re: Pls Help me ,simple work but Iam in Confusion.
 
Because of your English we could not understand what you are looking for ? Are you trying to read / write and what is the format you have the contacts in the file

Shilpa1 16Nov2007 10:37

Re: Pls Help me ,simple work but Iam in Confusion.
 
Thank you very much for ur reply.Iam doing Encryption and decryption of Contacts in Phone Book.In one dialog Iam performing encryption of Contacts.After Encryption,I want to write the Encrypted data to a File.In another Dialog I want to read the Encrypted data from the File line by line.How to do this?pls help me.I added the following code ,but always reading only the first line of File.I want to read line by line of the File.

Code:

unsigned char *encryptdata = NULL;
encryptdata =  new unsigned char[100];
Encrypt(encryptdata,dwDataLength,DKey);
//After Encryption,opening a File and writing the Encrypted data
 FILE * pFile;
pFile = fopen ("File.txt","a");
if (pFile!=NULL)
 {
fwrite (encryptdata , sizeof(encryptdata[0]) , 100 , pFile );
fputs("\r\n",pFile);
fclose (pFile);
 }


 //Open the File and get the data
 FILE * pFile;
 pFile = fopen ("File.txt","r");
char *buffer1 =new char[100];
 if (pFile!=NULL)
 {

 fgets(buffer1,100,pFile);
 fputs("\r\n",pFile);
 fclose (pFile);
 }


shabbir 16Nov2007 18:54

Re: Pls Help me ,simple work but Iam in Confusion.
 
You never loop through the file and so you will always get the first line. You should call the function fgets multiple time before the end of file is reached.

Salem 16Nov2007 20:53

Re: Pls Help me ,simple work but Iam in Confusion.
 
Does your Encrypt function always result in a printable string, or can it result in any binary data?

If it can result in binary data, then you need to use "rb" and "wb" modes for file access, and stick to fread() and fwrite().

> encryptdata = new unsigned char[100];
> Encrypt(encryptdata,dwDataLength,DKey);
Shouldn't you be allocating dwDataLength characters, and not just 100 ?

aVague 16Nov2007 23:56

Re: Pls Help me ,simple work but Iam in Confusion.
 
Try to use fscanf instead of fgets(fscanf automaticly read line by line and not only) and where are you from ? )


All times are GMT +5.5. The time now is 02:07.