Spacing out!!!!

This is a discussion on Spacing out!!!! within the C Programming forums, part of the General Programming Boards category; Below is a section of coding that I have done. My problem is that I want to repeat the process ...

  1. #1
    Olland
    Guest

    Spacing out!!!!

    Below is a section of coding that I have done. My problem is that I want to repeat the process which I know how to do however, I want to be able to write the data beside the orginal data which was already written to the file as is a table format. How can I do this with the code below.




    static void
    MyCopyFile(char *FromFile, char *ToFile)
    {
    FILE *from;
    FILE *to;
    char FileBuf[256];

    if ((from = fopen(FromFile, "r")) != NULL)
    {
    /* THE "a" WILL APPEND TO THE FILE, IF YOU WANT TO CREATE A NEW FILE, USE "w" */
    if ((to = fopen(ToFile, "a")) != NULL)
    {
    while (fgets(FileBuf, 255, from) != NULL)
    {
    fputs(FileBuf, to);
    }
    fclose(to);
    }
    else
    printf("Cannot open %s\n", ToFile);
    fclose(from);
    }
    else
    {
    printf("Cannot open %s\n", FromFile);
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Can you show what you mean, with a "before" and "after" example?

  3. #3
    Olland
    Guest

    Spacing out!!!!!!!!

    In reply to your question.
    Below is a section of coding that I have done. My problem is that I want to repeat the process which I know how to do however, I want to be able to write the data beside the orginal data which was already written to the file as is a table format. How can I do this with the code below.

    So the output will be put in a column form as seen below

    Name add phone no country id *****
    O 12/ add 5355646 IRE 54 *******
    P 13 4664747 ENG 23 *****
    R 34 2535656 WAl 78 *****
    and so on

    static void
    MyCopyFile(char *FromFile, char *ToFile)
    {
    FILE *from;
    FILE *to;
    char FileBuf[256];

    if ((from = fopen(FromFile, "r")) != NULL)
    {
    /* THE "a" WILL APPEND TO THE FILE, IF YOU WANT TO CREATE A NEW FILE, USE "w" */
    if ((to = fopen(ToFile, "a")) != NULL)
    {
    while (fgets(FileBuf, 255, from) != NULL)
    {
    fputs(FileBuf, to);
    }
    fclose(to);
    }
    else
    printf("Cannot open %s\n", ToFile);
    fclose(from);
    }
    else
    {
    printf("Cannot open %s\n", FromFile);
    }
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    You have to append the data to the line before you write it
    Like so

    Code:
    while (fgets(FileBuf, 255, from) != NULL)  { 
        char *p = strchr( FileBuf, '\n' );
        if ( p ) *p = '\0';   // this removes the newline
    
        // here is where you add your data
        // **** is your data, and \n is a newline
        // to replace the one you just removed
        strcat( FileBuf, "****\n" );
    
        // now output the file as normal
        fputs(FileBuf, to); 
    }
    You probably want to open a new file in "w" mode.

    "a" is for appending whole lines to the end of the file, not appending bits of lines to the end of each line

  5. #5
    Olland
    Guest

    Angry spacing out!!

    Thanks for you help, however when I posted the data it seemed to get corrupted.

    I Want to use the above code to produce a column format, eg

    Name ,address ,phone no , details ,
    John ,111 *** , 737347 , yer,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Better spacing issues
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2008, 03:46 PM
  2. lowest spacing between numbers
    By Ene Dene in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2007, 03:08 PM
  3. Dev C++ Spacing Problem
    By Darklighter137 in forum C++ Programming
    Replies: 7
    Last Post: 11-13-2006, 09:48 AM
  4. Spacing between printf statements
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 08-03-2006, 10:02 AM
  5. Spacing?
    By trenzterra in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21