So I run this in the terminal, give it two filenames and I get a segmentation error, basically it's supposed to open two files, and print a line from file1 then print a line from file2 until an EOF. Where am I getting a pointer problem?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 1000
int main(int argc, char *argv[])
{
int i = 0, j = 0;
char ch, ct;
// char fname[MAX];
FILE *fo, *ft;
char line1[MAX];
char line2[MAX];
if (argc != 3)
{
printf("Usage: %s filename \n", argv[0]);
exit(1);
}
if ((fo = fopen(argv[1], "r")) == NULL)
{
printf("Can't open %s filename\n", argv[1]);
exit(1);
}
if ((ft = fopen(argv[2], "r")) == NULL)
{
printf("Can't open %s filename\n", argv[1]);
exit(1);
}
while ((ch = getc(fo)) != EOF && (ct = getc(ft)) != EOF)
{
i = j = 0;
line1[i] = ch;
i++;
while ((ch = getc(fo)) != '\r')
{
line1[i] = ch;
i++;
}
line1[i] = '\0';
puts(line1);
line2[j] = ct;
j++;
while ((ct = getc(ft)) != '\r')
{
line2[j] = ct;
j++;
}
line2[j] = '\0';
puts(line2);
}
fclose(fo);
fclose(ft);
return 0;
}