Pls Help me ,simple work but Iam in Confusion.

Discussion in 'C' started by Shilpa1, Nov 15, 2007.

  1. Shilpa1

    Shilpa1 New Member

    Joined:
    Nov 8, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
     
  3. Shilpa1

    Shilpa1 New Member

    Joined:
    Nov 8, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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);
     }
     
    Last edited by a moderator: Nov 16, 2007
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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 ?
     
  6. aVague

    aVague New Member

    Joined:
    May 2, 2007
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    forex
    Try to use fscanf instead of fgets(fscanf automaticly read line by line and not only) and where are you from ? )
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice