Hi,
I just came across this program on the net and modified it a bit, but am not getting the same output.Could you'll please help me.
P.S: This may be a silly one, but please bear with me as im a new programmer.Thank you.
program to create a sequential file and to store data in a tabular form:
[COLOR="Blue"]
Code:
#include <stdio.h>
#include <string.h>
#define FILENAME "seqtxtfi.txt"
int main(void)
{
int n,i;
char fld1[25];
char fld2[20];
char fld3[15];
char fld4[15];
FILE *fp;
printf("Enter Ctrl-D to end input.\n");
if ((fp = fopen(FILENAME, "w")) == NULL)
printf("File could not be opened\n");
else
printf("Enter the no. of flds\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{ start:
printf("enter no.");
scanf("%d",&n);
printf("Enter Field 1 : ");
fgets(fld1, sizeof(fld1), stdin);
if (feof(stdin)) return 0;
fld1[strlen(fld1)-1] ='\t';
printf("Enter Field 2 : ");
fgets(fld2, sizeof(fld2), stdin);
if (feof(stdin)) return 0;
fld2[strlen(fld2)-1] ='\t';
printf("Enter Field 3 : ");
fgets(fld3, sizeof(fld3), stdin);
if (feof(stdin)) return 0;
fld3[strlen(fld3)-1] ='\t';
printf("Enter Field 4 : ");
fgets(fld4, sizeof(fld4), stdin);
if (feof(stdin)) return 0;
fld4[strlen(fld4)-1] ='\0';
{
fprintf(fp, "%s%s%s%s\n", fld1, fld2, fld3, fld4);
goto start;
break;
}
fclose(fp);
}
return 0;
}
I expect the output to be like:
fld1 fld2 fld3 fld4
1 2 3 4
3 4 5 6
Unfortunately, i get Enter fld1 : Enter fld2:
i.e the first fld1 does not get accepted. Later on its ok. Plz help me.Thanks in advance.