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..