Question:
how to read and write line by line in C?
Danielle C
2008-07-01 19:59:53 UTC
I am struggling writing this program: a program that takes two text files as input from the command line and reformats them into a third file in the following manner: it reads line by line from the two files and merges lines alternatively from these two files and writes them into the third file. But lines from the first file should be merged with random lines from the second file. For instance, you roll a dice to pick a line number from the second file that is to be merged with line 10 of the first file.

I got this so far any help:

#include
#include
#include

int main ( void )
{
char filename[] = "data.txt";
char filename2[]= "data1.txt";
FILE *file = fopen ( filename, "r" );
FILE *file2 = fopen ( filename2,"r");
int i, j;
char arra[128][128];
char arra2[128][128];
char line[128];
char line2[128];

if ( file != NULL )
{

i=0;

while ( fgets ( line, sizeof line, file ) != NULL) /* read a line */
{

......
Three answers:
cja
2008-07-02 10:25:15 UTC
You skipped the first part of the assignment, where it asks you to get the file names from the command line. Adding that is pretty easy. I'm sure you can see that conceptually it's a pretty straightforward program, clearly described by the assignment instructions, but I guess you're having some trouble constructing it. My code below will do exactly what you need.



Study it well, understand what I'm doing and why, and I'm sure you'll learn something. Don't pass my code off verbatim as your own, but feel free to incorporate my design and techniques into your own solution.



#include

#include

#include

#include



#define MAX_FILE_LINES 1024

#define MAX_LINE_LEN 256



#define OUT_FILENAME "text.out"



void readFile(FILE *,char *text[],int *len);

void printFile(char *text[],int len);

int random(int range);



int main(int argc, char *argv[]) {

char *file1Text[MAX_FILE_LINES];

char *file2Text[MAX_FILE_LINES];

FILE *fp1, *fp2, *fpOut;

int f1LineCount, f2LineCount;

int i,x;

time_t t;



srand((unsigned)time(&t));



if (argc < 3) {

printf("\nusage: %s \n",argv[0]);

exit(EXIT_FAILURE);

}

if ((fp1 = fopen(argv[1],"r")) == NULL) {

printf("\nerror: failed opening %s\n",argv[1]);

exit(EXIT_FAILURE);

}

if ((fp2 = fopen(argv[2],"r")) == NULL) {

printf("\nerror: failed opening %s\n",argv[2]);

exit(EXIT_FAILURE);

}

if ((fpOut = fopen(OUT_FILENAME,"w")) == NULL) {

printf("\nerror: failed opening %s\n",OUT_FILENAME);

exit(EXIT_FAILURE);

}



readFile(fp1,file1Text,&f1LineCount);

readFile(fp2,file2Text,&f2LineCount);



printf("\nContents of %s (%d lines):\n",argv[1],f1LineCount);

printFile(file1Text,f1LineCount);

printf("\nContents of %s (%d lines):\n",argv[2],f2LineCount);

printFile(file2Text,f2LineCount);

fclose(fp1);

fclose(fp2);



for (i = 0; i < f1LineCount; i++) {

x = random(f2LineCount-1);

printf("\nwriting %s line %d + %s line %d to %s",

argv[1],i,argv[2],x,OUT_FILENAME);

fprintf(fpOut,"%s : %s\n",file1Text[i],file2Text[x]);

}

fclose(fpOut);



for (i = 0; i < f1LineCount; i++) free(file1Text[i]);

for (i = 0; i < f2LineCount; i++) free(file2Text[i]);



return 0;

}



void readFile(FILE *fp,char *text[],int *len) {

char line[MAX_LINE_LEN];



*len = 0;

while (fgets(line,MAX_LINE_LEN,fp) != NULL) {

text[*len] = (char *)calloc(strlen(line),sizeof(char));

*strchr(line,'\n') = '\0';

strcpy(text[(*len)++],line);

}

}



void printFile(char *text[],int len) {

int i;

for (i = 0; i < len; i++) {

printf("%s\n",text[i]);

}

}



int random(int range) {

float x = ((float)rand() / (float)RAND_MAX) * range;

return (int)x;

}
Mike B
2008-07-01 20:19:10 UTC
I recommend cplusplus.com's forum. People are very helpful though you will need to state what you're precise problem is. People won't just write your code for you
anonymous
2014-12-04 20:49:10 UTC
tricky aspect. look at the search engines. it might help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...