I am new at coding. I wanted to read from a file(text) and then write it to another file many times so that the file I write to becomes as large as 20MB..I read Harry Potter book for that....the code works just fine for 1000 lines etc..the file is read properly...but when i read the whole book, it reads ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ...insted of the last few paragraphs(5-6)...why does it include garbage values for just the last few paragraphs??
Kindly help..

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>

char *textread();
void textwrite(char *text, int size);
int main()
{ 
    char *text;
    text=textread();
    textwrite(text,strlen(text));
    delete text;
    //getch();
    return -1;
}
char *textread(){
    FILE *f_init;
    f_init = fopen("C:\\data\\init_file.txt","r");
    fseek(f_init, 0, SEEK_END);
    int init_file_size = ftell(f_init);

    fclose(f_init);
    f_init = fopen("C:\\data\\init_file.txt","r");

    char *text;
    text = new char[ init_file_size+1];
    text[ init_file_size]='\0';
                if(f_init!= NULL)
                {
                    fread( text, sizeof(char),  init_file_size, f_init);
                }

     fclose(f_init);

      return text;
} 
void textwrite(char *text, int size){
    FILE *f_main;
    f_main = fopen("C:\\data\\main_file.txt", "a+");
    for(int i=0; i<2;i++)
    fwrite(text,sizeof(char),size,f_main);

    fclose(f_main);

}

I have used the loop just 2 times as large files do not open..
I am compiling on visual studio...so the code may include some strange libraries..

Recommended Answers

All 6 Replies

Well, first off, regarding the libraries you've included, I would recommend using the C++ versions rather than the C-style names. All standard C++ headers now omit the '.h' extension, and for the older C headers, prepend them with the letter 'c'. Thus, <stdlib.h> should now be <cstdlib>, <string.h> should be <cstring> and so forth. This change was made over a decade ago, and I am surprised that the Visual C++ compiler doesn't give a warning about that.

On a related note, the <conio.h> header is a proprietary one, and deprecated. It was specific to the oldr DOS console, and while the Windows console is similar, there are significant differences. Since the only place you actually used anything in it is commented out, I would recommend removing it completely.

Is there a particular reason you are using the C-style file I/O instead of the C++ iostreams? The C++ style file I/O is generally a lot easier to use. In this case I doubt it would make much of a difference, but it is worth asking about.

I notice that you close the file and then open it again immediately afterwards. This appears to be used mainly to reset the file position back to the start of the file. This shouldn't be necessary, as you can avoid a lot of overhead by using the following line of code instead:

fseek(f_init, 0, SEEK_SET);

Since you use fseek() earlier, I am surprised that you didn't do it this way.

Finally, I tested this myself and found that it worked when applied to a relatively small file without trouble, though it copied the file twice into the main_file.txt. Does this problem only occur with larger files?

it is # include<iostream.h>
as u r reading from a file it is eof @ null

commented: Actually, it *is* <iostream> - <iosteam.h> is the obsolete form. -1

you should put in a buffer like 1000 lines, copy that into the new file, and clear the buffer, and do it again. You see, after some time, it will fill up the buffer.
you can put a condition using of course c++ libraries:

while (getline(f_in, line)){
    //do stuff
    if lines == 1000
        clear the buffer.
        and continue
}

Schol-R-LEA, thank you..
yes, that problem occurs with only large files..the file i read was 967 KB..

Lucaci, does the problem occur coz i dnt put in a buffer??

Either way, you will be using a buffer:
If you want to know what the buffer is: Click Here

Thank you Lucaci..:)
I appreciate your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.