Redirection

This is a discussion on Redirection within the C Programming forums, part of the General Programming Boards category; I'm teaching myself C programming for a personal project, and when I run this program, the file (array1.cpp) is sent ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22

    Smile Redirection

    I'm teaching myself C programming for a personal project, and when I run
    this program, the file (array1.cpp) is sent to the destination file
    (LIST E.cpp) but not the output of the array. I tried (array1.exe) but
    sent garbled dribble. I'm only trying to send the output of the array to the destination file (LIST E.ccp). Can anyone help, been at this for weeks? Thanx Rossco

    Code:
    #include <stdio.h>
    #include <cstdlib>
    
        main()
    {
    
        int list[3][6] = { 1,  2,  3,  4,  5,  6,
                           7,  8,  9, 10, 11, 12,
                          13, 14, 15, 16, 17, 18}; 
    
        int i, j;
        i = 0;
      
        for(j = 0; j < 6; j++) 
          printf( "%4d", list[i][j]);
          printf( "\n"); 
         
        for( j = 0; j < 12; j++) {
           if((j>4)&&(j<11))
           continue;
         printf( "%4d", list[i][j]);} 
         printf( "\n");
        
        for(j=0; j<18; j++) {
             if((j>4) && (j<17))
         continue;
        printf( "%4d", list[i][j]); }
        printf( "\n");
        
       enum  {SUCCESS, FAIL};  
       void CharReadWrite(FILE *fin, FILE *fout);
    
    // defines two file pointers
        FILE *fptr1, *fptr2;
        char filename1[] = "LIST E.cpp"; //array 1 initiated with filename
        char filename2[] = "array1.cpp"; //writes to the above file
        int reval = SUCCESS;
    
        if((fptr1 = fopen(filename1, "w")) == NULL){                    
        printf( "Cannot open: %s\n", filename1);
        reval = FAIL; 
        }
         
         else if ((fptr2 = fopen(filename2, "r")) == NULL){
        printf( "Cannot open %s\n", filename2);
        reval = FAIL;
        } else {
          
        CharReadWrite(fptr2, fptr1);
        fclose(fptr2);
        fclose(fptr1);
        }
        system ("PAUSE");
        return reval;
        }
        void CharReadWrite(FILE *fin, FILE *fout)
        {
        int c;
        
        
        while((c=fgetc(fin)) != EOF) { 
        fputc( c, fout); 
        putchar(c);  
        }
        }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Poor choice of file names imo. You should keep .cpp files to have C++ source code files and not edit them with your program (unless you know what you're doing. Having a program modify its source code can be cool, but it'll really mess things up if it makes an unexpected move.)

    What exactly are you trying to do?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    As you can tell I have no formal training...C++ compiler is the only one I have, and I have found
    C language easier to understand for a beginner.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Nearly every C++ compiler in the world has a switch to make them a C compiler.

    You are mixing C++ and C style things in your code, which means that it won't compile (cleanly at least) in a non-C++ compiler, which is never a good thing.

    Put all your variable declarations at the top. And if you are not strictly going to edit C++ files, call them for example "array1.txt" or "array1.lst" or some such, rather than "array1.cpp".

    cstdlib is the "C++" version of stdlib.h

    You should put braces around your 2D array initialization:
    Code:
       int list[3][6] = { { 1,  2,  3,  4,  5,  6} ,
    		      { 7,  8,  9, 10, 11, 12 },
                          { 13, 14, 15, 16, 17, 18 }};
    Also, declare main as "int main()".

    And your indentation needs some work to understand what is going on.

    What array is it you want sent to which file?

    --
    Mats
    Last edited by matsp; 11-23-2007 at 05:51 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Linux file redirection buffering issue !!!!
    By AirSeb in forum Linux Programming
    Replies: 8
    Last Post: 03-14-2009, 05:32 PM
  3. Implement Redirection for a simple shell
    By koooee in forum C Programming
    Replies: 4
    Last Post: 02-22-2009, 02:21 PM
  4. General a.out redirection question under Linux
    By merixa in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2005, 04:36 PM
  5. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM

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