Thread: Reading from file and writing to a different file

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    2

    Reading from file and writing to a different file

    When I try to copy one file over to another file, character by character, a new output file is created but nothing copied over. Does anyone know what I am doing wrong?

    Code:
    fopen_s(&filein, argv[i], "rb");    //open source file
            sourceLength = strlen(argv[i]) + 1;    
            strcpy_s(sourceFilename, 100, argv[i]);    
    
    
    //Manipulate source filename by removing the extension
    for (int i = sourceLength; i >= 0; i--)    //starting from 
            {
                if (sourceFilename[i] == '.')
                {
                    sourceFilename[i] = 0;    //append null byte 
                    break;
                }
            }
    
    
            strcat_s(sourceFilename, 100, ".bak");    //concatenate 
    
            fopen_s(&fileout, sourceFilename, "wb");    //open output
    
            if (fileout == NULL)
            {
                printf("Cannot open target file.");    //output error 
                fclose(fileout);
                exit(1);
            }
    
    
            while (ch != EOF)
            {
                ch = getchar();    //read character from source file
    
                if (ch == EOF)
                {
                   break;    //break out of while loop if EOF is 
                }
               else
                {
                    putchar(ch);    //wirte character from source file 
                }
    }

  2. #2
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    I think one of the problems here is that you are reading the characters from the default input channel in c. Read about these fucntions:

    C library function - fputc()
    C library function - getc()
    C library function - getchar()


    Also take a look at this examples it's very similar to what you want to achieve
    C library function fopen()
    I have not tested it because I do not have the whole program but perhaps these changes will point in the right direction
    Code:
    while (ch != EOF) {
        ch = getc(filein);    //read character from source file
        fputc(ch, fileout);    //wirte character from source file 
    }

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    2
    Sweet! You were right, fredlo. Thanks so much!

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    You are welcome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and Writing to a file
    By m_programmer in forum C Programming
    Replies: 5
    Last Post: 10-18-2012, 12:27 PM
  2. need some help with writing and reading to/from a file
    By 999cm999 in forum C Programming
    Replies: 32
    Last Post: 12-18-2011, 10:04 AM
  3. reading and writing to a file
    By rocketman50 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2010, 12:36 PM
  4. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 02:17 PM
  5. Reading and Writing to a file
    By Granger9 in forum C Programming
    Replies: 0
    Last Post: 12-30-2002, 08:31 AM

Tags for this Thread