Sorry for asking very simple question.I’m new to coding.
Input txt file
5,3
001
110
111
110
001
I need to print the output as
U1: 001
U2: 110
U3: 111
U4: 110
U5: 001
Upto now I was able to print the contents with this:
#include <stdio.h>
void main() {
FILE *fopen(), *fp;
int c;
fp = fopen("read.txt","r");
c = getc(fp) ;
while (c!= EOF) {
putchar(c);
c = getc(fp);
}
fclose(fp);
}
Can Anybody tell how should I proceed?
Most of your work is done inside the
whileloop.But you’re not doing enough work …
a) you need to count lines
b) you need to print the
"U#"Suggestion: create a variable for the line counting business and rewrite your loop to consider it. Here’s a few snippets
Oh! You really shouldn’t add the prototype for
fopenyourself. It is already specified with#include <stdio.h>.And well done for declaring
casint. Many people do the error of declaring itcharwhich is incompatible withEOFand all the range of characters