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.
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
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); }
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.
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 ?
Try to use fscanf instead of fgets(fscanf automaticly read line by line and not only) and where are you from ? )