So I have a text file (called So.txt) that has in it:
Will
Happy
12.50
2012
Kanye
Wolves
15.99
2016
I'm writing a program that reads from it and adds a certain prefix to each read line. Basically I would like the output to look like this:
Name of singer: Will
Name of song: Happy
Price: 12.50
Year of release:2012
Name of singer: Kanye
Name of song: Wolves
Price: 15.99
Year of release:2016
This is the program.
song_file = fopen("So.txt", "r");
char singleline[150];
while (!feof(song_file) ) {
fgets(singleline, 150, song_file);
puts(singleline);
printf("Name of singer: ");
printf("Name of song: ");
printf("Price: ");
printf("Year of release: ");
}
I think your target is output like the below code.
Maybe you can have a look, and we can discuss it later on.
#include <stdio.h>
#include <string.h>
#define STRLEN 10
char Prefix_word[][20]={
"Name of singer :",
"Name of song : ",
"Price : ",
"Year of release : "
};
void main(void){
FILE *fd;
fpos_t Current_pos;
fpos_t Start_pos;
char singleline[STRLEN];
int index=0;
int len=0;
fd = fopen("test.txt", "r");
/* Get the Start position */
fgetpos(fd, &Start_pos);
/* Initial string array */
memset(singleline, ' ', STRLEN);
while(!feof(fd)){
/* Counting the lenght of string */
len++;
/* Read 1 character to check whether to go to the next column*/
fread(singleline, sizeof(char), 1, fd);
if(singleline[0]=='\n'){
fgetpos(fd, &Current_pos);
fsetpos(fd, &Start_pos);
fread(singleline, sizeof(char), len, fd);
/* Print */
printf("%s %s\n",Prefix_word[index++], singleline);
/* Get Start position */
fgetpos(fd, &Start_pos);
/* Initial */
len=0;
memset(singleline, ' ', STRLEN);
if(index>=4){
printf("================\n");
index=0;
}
}
}
fclose(fd);
}