C program complies but screen on console remains black
C program complies but screen on console remains black
I was writing a C program that would read and merge 3 files together (program not complete yet), however, as I was testing I realized the program compiles but the screen on the console remains blank!
Any help is appreciated, especially why is it blank?
#include <stdio.h>
#include <stdlib.h>
int main()
printf("test");
//open three files for merging
FILE *fp1 = fopen("american0.txt","r");
FILE *fp2 = fopen("american1.txt","r");
FILE *fp3 = fopen("american2.txt","r");
printf("test");
//open file to store the result
FILE *fpm = fopen("words.txt", "w");
//creating an array to save the files data
char c;
char mergedFile[50];
//checking to make sure files are being read
if(fp1 == NULL && fp2 == NULL && fp3 == NULL && fpm == NULL)
printf("Could not open one or all of the files.n");
printf("Exiting program!");
exit(0);
printf("test");
//initializing counter values
//inserting data from file into an array
while ((c = fgetc(fp1)) != EOF)
fputc(c, mergedFile);
while ((c = fgetc(fp2)) != EOF)
fputc(c, mergedFile);
while ((c = fgetc(fp3)) != EOF)
fputc(c, mergedFile);
printf("%s",mergedFile[0]);
printf("test");
return 0;
fputc
FILE*
mergedFile
char mergedFile[50];
fpm
mergedFile
printf("%s",mergedFile[0]);
%s
const char*
char
printf
Well I compiled and ran it and it did print stuff out, not sure what might be going wrong on your end. Don't take this wrong, as this feels like a stupid question to ask, but did you actually run it after compiling? Compiling will not execute the program, it will just produce a program that you can execute.
– pstrjds
Sep 15 '18 at 16:10
I feel like it is something minor and stupid that I'm doing that's causing this, However, I compile the program, all the warnings print out and when I run it the using "./a.out" it doesn't print out anything
– mahdi
Sep 15 '18 at 16:32
I also just put the printf("%s", mergeFile[0]); as a test statement which I realized was wrong, but thanks for pointing out the issue with the mergedFile in the while statements :)
– mahdi
Sep 15 '18 at 16:34
I believe the issue was with me passing mergedfile in the while loops, thank you for your help
– mahdi
Sep 15 '18 at 16:38
1 Answer
1
Error --> fputc
requires a file pointer as it's second argument rather than an array: int fputc ( int character, FILE * stream );
fputc
int fputc ( int character, FILE * stream );
Points to be taken care of:
char
Here is a minimal corrected version:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 //ADDED NEW
int main()
//open three files for merging
FILE *fp1 = fopen("american0.txt","r");
FILE *fp2 = fopen("american1.txt","r");
FILE *fp3 = fopen("american2.txt","r");
//open file to store the result
FILE *fpm = fopen("words.txt", "w");
//creating an array to save the files data
int c;
int i=0;
char mergedFile[MAX]=0; //MODIFIED & INITIALIZED
//checking to make sure files are being read
if(fp1 == NULL && fp2 == NULL && fp3 == NULL && fpm == NULL)
printf("Could not open one or all of the files.n");
printf("Exiting program!");
exit(0);
//initializing counter values
//inserting data from file into an array
while (((c = fgetc(fp1)) != EOF)&&(i<MAX)) //MODIFIED
mergedFile[i++]=c; //MODIFIED
while (((c = fgetc(fp2)) != EOF)&&(i<MAX)) //MODIFIED
mergedFile[i++]=c; //MODIFIED
while (((c = fgetc(fp3)) != EOF)&&(i<MAX)) //MODIFIED
mergedFile[i++]=c; //MODIFIED
printf("%s",mergedFile); //MODIFIED
return 0;
Better to use
int c;
than char c;
to properly handle the typical 256 + 1 different results from fgetc()
.– chux
Sep 15 '18 at 17:14
int c;
char c;
fgetc()
@chux thanks, corrected that!
– Observer
Sep 15 '18 at 17:17
I know you were copying OP's code, but I think that
if
statement checking for file opening success should be ||
not &&
since there will be a problem later if any file didn't open, not just if all didn't open.– pstrjds
Sep 15 '18 at 18:41
if
||
&&
@user10334659 - I know that is what the OP has written (which is why I prefaced my comment with that), but since you are fixing the code I figured it was worth pointing out. As written it will only be true if all of the file open calls fail. If even one of them is successful the code will continue, which will cause problems later.
– pstrjds
Sep 15 '18 at 18:49
Thank you so much for all your help. this cleared up a lot of confusion!
– mahdi
Sep 16 '18 at 3:42
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
fputc
requires aFILE*
as the second argument. How on earth did you get this to even compile when passingmergedFile
, declared aschar mergedFile[50];
to those function calls ? Shouldn't those all usefpm
? From what I see,mergedFile
is pointless to have in this code at all. Removing it and all it's usages would also fixprintf("%s",mergedFile[0]);
, which makes no sense itself, as%s
requires aconst char*
, and you're passing achar
to thatprintf
invoke.– WhozCraig
Sep 15 '18 at 16:05