vishalkhialani
03-26-2007, 12:45 AM
Hi,
I am unable to get the logic correct to get the data out from a file. Please advice your suggestions.
#include<stdio.h>
#include <string.h>
FILE *f;
int count;
int choice;
int style;
int some=0;
char answer='y';
struct attributes
{
int style;
char buyerName[40];
}att;
int input()
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("PURCHASE ORDER SYSTEM \n");
printf("MAIN MENU ------- INPUT / EDIT OPTION \n\n\n\n\n\n");
printf("PLEASE ENTER STYLE NUMBER \n");
scanf("%d",&att.style);
printf("PLESE ENTER THE BUYER'S NAME \n");
scanf("%s",&att.buyerName);
f=fopen("data.txt","a");
if(f==NULL)
{
printf("ERROR");
return 0;
}
else
{
fprintf(f,"%d",att.style);
fprintf(f,"%s",att.buyerName);
fclose(f);
printf("RECORDS HAVE BEEN UPDATED. PLEASE ENTER A NUMBER TO GO BACK TO MENU");
scanf("%d",&some);
return 0;
}
}
int read()
{
f=fopen("data.txt","r");
if(f==NULL)
{
printf("ERROR");
return 0;
}
else
{
// I AM GETTTING STUCK HERE I DONT KNOW HOW TO GET THE DATA
// FROM THE FILE AND PUT IT IN THE STRUCTURE.
while(fscanf(f,att,&att)!=EOF)
{
printf("%d",att.style);
printf("%d",att.buyerName);
}
fclose(f);
return 0;
}
}
int main()
{
while(count<9)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("PURCHASE ORDER SYSTEM \n");
printf("MAIN MENU ------- INPUT \n\n\n\n\n\n");
printf("PLEASE SELECT ONE OF THE GIVEN OPTIONS \n");
printf("1) ENTER NEW STYLE DETAILS \n");
printf("2) READ DATA \n");
printf("3) EXIT \n");
scanf("%d",&choice);
if(choice==1)
input();
if(choice==2)
read();
if(choice==3)
return 0;
else
printf("\n\n\n\n\n ERROR YOU HAVE ENTERED A WRONG CHOICE\n\n\n");
}
}
I am unable to get the logic correct to get the data out from a file. Please advice your suggestions.
#include<stdio.h>
#include <string.h>
FILE *f;
int count;
int choice;
int style;
int some=0;
char answer='y';
struct attributes
{
int style;
char buyerName[40];
}att;
int input()
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("PURCHASE ORDER SYSTEM \n");
printf("MAIN MENU ------- INPUT / EDIT OPTION \n\n\n\n\n\n");
printf("PLEASE ENTER STYLE NUMBER \n");
scanf("%d",&att.style);
printf("PLESE ENTER THE BUYER'S NAME \n");
scanf("%s",&att.buyerName);
f=fopen("data.txt","a");
if(f==NULL)
{
printf("ERROR");
return 0;
}
else
{
fprintf(f,"%d",att.style);
fprintf(f,"%s",att.buyerName);
fclose(f);
printf("RECORDS HAVE BEEN UPDATED. PLEASE ENTER A NUMBER TO GO BACK TO MENU");
scanf("%d",&some);
return 0;
}
}
int read()
{
f=fopen("data.txt","r");
if(f==NULL)
{
printf("ERROR");
return 0;
}
else
{
// I AM GETTTING STUCK HERE I DONT KNOW HOW TO GET THE DATA
// FROM THE FILE AND PUT IT IN THE STRUCTURE.
while(fscanf(f,att,&att)!=EOF)
{
printf("%d",att.style);
printf("%d",att.buyerName);
}
fclose(f);
return 0;
}
}
int main()
{
while(count<9)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("PURCHASE ORDER SYSTEM \n");
printf("MAIN MENU ------- INPUT \n\n\n\n\n\n");
printf("PLEASE SELECT ONE OF THE GIVEN OPTIONS \n");
printf("1) ENTER NEW STYLE DETAILS \n");
printf("2) READ DATA \n");
printf("3) EXIT \n");
scanf("%d",&choice);
if(choice==1)
input();
if(choice==2)
read();
if(choice==3)
return 0;
else
printf("\n\n\n\n\n ERROR YOU HAVE ENTERED A WRONG CHOICE\n\n\n");
}
}