PDA

View Full Version : Getting data out from a file


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");



}

}

KeZZeR
03-26-2007, 09:24 AM
std::string getvalue(std::fstream *configFile, std::string attribute)
{
std::string line;

for(int i = 0; !configFile->eof(); i++)
{
std::getline(*configFile, line); // retrieves the current line

int indexPos = line.find(attribute, 0); // finds the matching key given "attribute" argument

if(indexPos != -1)
{
return line.substr((line.find("=", indexPos + attribute.length()) + 2), line.length()); // get the actual value, doing indexValue + 2 because there should be a space.
}
}

return "";
}

Here's a function I wrote recently to get values from a text file, it's not specific to what you're doing and I can't be arsed to rewrite it for you, but it gives you the general idea of things.

oracleguy
03-26-2007, 06:05 PM
std::string getvalue(std::fstream *configFile, std::string attribute)
{
std::string line;

for(int i = 0; !configFile->eof(); i++)
{
std::getline(*configFile, line); // retrieves the current line

int indexPos = line.find(attribute, 0); // finds the matching key given "attribute" argument

if(indexPos != -1)
{
return line.substr((line.find("=", indexPos + attribute.length()) + 2), line.length()); // get the actual value, doing indexValue + 2 because there should be a space.
}
}

return "";
}

Here's a function I wrote recently to get values from a text file, it's not specific to what you're doing and I can't be arsed to rewrite it for you, but it gives you the general idea of things.

And just to be clear for the OP, this code is C++ and can't be just plugged into your code.

Also vishalkhialani, please use the [code ] [/ code] tags (without the spaces in them) like KeZZeR did when you post code in the future.