Thread: Problem in File Process

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    5

    Problem in File Process

    the program decodes the data(three lines) in a file and writes to an other file. but there are one line in the second file.so the program writes three lines as one line. how can I write as three lines to second file

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I think what you are asking is how to add carriage return and line feed to the end of each line. These characters are '\r' and '\n' respectively.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    post some code ..must be some minor error somewhere..

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    this is the code;
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>

    int main()
    {

    char coded[35];
    int decode[35],i;
    FILE * p;

    p=fopen("message.dat","r"); // Opens the file with the coded text

    for (int c=0;c<35;c++)
    {
    coded[c]=getc(p);
    cout<<coded[c];
    }printf("\n\n");


    for(i = 0; i < 35; i++)
    {
    decode[i] = coded[i];
    decode[i] -= 2;

    }

    p=fopen("unco.dat","w"); // Opens a new file with the decoded text
    for (int cc=0;cc<35;cc++)
    {
    putc(char(decode[cc]),p);
    cout << char(decode[cc]);
    }

    getche();
    return 0;

    }
    Last edited by ercumania; 07-24-2006 at 05:47 AM.

  5. #5
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Use code tags henceforth...
    > how can I write as three lines to second file <
    Where are u opening ur second file ?
    And what kind of arrays are coded and decoded ?.. U dont even need these arrays if u r just trying to transfer the contents of one file to another
    What exactly are u trying to do ?

  6. #6
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    better now,
    Just remember that u need to set the parameter that catches the value that fgetc() returns as int... READ the FAQ for more details...

    I think u can go with this for starters :
    I am using C ..hope thats not a problem...
    Code:
      int  main()
    {
     int  c;
     FILE *fp1,*fp2;
     fp1=fopen("file1.txt","r");
      fp2= fopen("file2.txt" ,"w");
     printf("contents of file1.txt \n");
                                                                                                                                 
     while((c=getc(fp1))!=EOF)
       {
                                                                                                                                 
          fputc( c , fp2);
          printf("%c",c);
       }
     fclose(fp1);
     fclose(fp2);
     
     fp2=fopen("file2.txt","r");
     printf(" contents of file2.txt \n");
                                                                                                                                 
     while((c=getc(fp2))!=EOF)
       {
                                                                                                                                 
        printf("%c",c);
       }
      fclose(fp2);
    
    return 0;
    }
    Didnt compile it . but, have a strong feeling it should work
    If u feel reading it char by char is too boring , u can think of fread and fwrite..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM