The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Noob help with text files
Discuss Noob help with text files in the C Programming forum on Dev Shed. Noob help with text files C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 11th, 2010, 02:50 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 6
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
|
|
|
Noob help with text files
Hi im trying to create a program for a store. I isolated the part of the program by making the rest of the program comments. What i am trying to do is read in the name of the product the price of the product and how much is in stock, I want to write this to a textfile and then print it out to the screen but it doesnt seem to be working. I limited the amount of products to 2 for testing reason. Any help would be appreciated.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
/*char menu
{
int choice
while(choice != 'e')
printf("Welcome to the REGISTER 2000\n");
printf("Choose the corresponding letter to choose the option you desire");
printf("a. Payment\n");
printf("b. Change price\n");
printf("c. Veiw stock\n");
printf("d. Add stock\n");
printf("e. Exit");
fflush(stdout);
scanf("%c", &choice);
}
int password
{
char userid[]="zion",password[]="zionatscodz",p[15],u[15];
int n=1,a,b;
printf("Enter USER ID and PASSWORD below (You have only three chances to enter)\n");
getchar();
while(n<=3)
{
printf("\nUSER ID: ");
scanf("%s",u);
printf("\nPASSWORD: ");
scanf("%s",p);
a=strcmp(u,userid);
b=strcmp(p,password);
if(a==0&&b==0)
{
printf("You have logged in successfully.\n");
break;
}
else
{
printf("Wrong PASSWORD and/or USER ID. Now you have %d more chances ", 3-n);
}
getchar();
n++;
}
if(n==4)
printf("You can't log in.\n");
getchar();
}
*/
main()
{
main()
{
int i;
float price;
char item[10];
int stock;
FILE * fp =fopen("price list.txt","r+");
if(!fp)
{
printf("the file could not be found, create a file\n");
getchar();
return 1;
}
printf("Input prices\n");
for (i=1;i<=2;i++)
{
fscanf(stdin, "%s %f %d",item, price, stock);
fprintf(fp,"%s %f %d\n",item, price, stock);
}
fclose (fp);
fprintf(stdout,"\n");
fp=fopen("price list.txt","r+");
printf("Item price stock\n");
for(i=1;i<=3;i++)
{
fscanf(fp,"%s %f %d\n",item, &price, &stock);
fprintf(stdout,"%s %f %d\n",item, price, stock);
}
fclose(fp);
system("PAUSE");
}
|

January 11th, 2010, 03:09 PM
|
|
|
__________________
I ♥ ManiacDan & requinix
This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!
|

January 11th, 2010, 03:11 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 6
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
|
|
|
well i was fiddling around with it trying to get it to work but no luck
|

January 11th, 2010, 04:57 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
|
Go back and reformat your code. Use proper indentation and you just might get some assistance with this.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/
|

January 11th, 2010, 04:58 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 6
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
|
|
|
what do you mean reformat the code?
|

January 11th, 2010, 05:02 PM
|
|
|
|
You're trying to be a cut-and-paster. That ways lies failure. Make an effort to learn what it is you're doing.
|

January 11th, 2010, 05:04 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
Quote: | Originally Posted by jazdaboss I want to write this to a textfile and then print it out to the screen but it doesnt seem to be working |
Really? What does it seem to do instead?
Read this:
http://forums.devshed.com/c-program...rst-259106.html
Note that you are failing to check the return value from fscanf() before trying to write what may be uninitialized data.
Did you really intend for your output to have a different format than your input? Check the number of spaces between type specifiers in your format strings.
Last edited by jwdonahue : January 11th, 2010 at 05:10 PM.
|

January 11th, 2010, 05:06 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
|
I mean reformat the code. It's not properly indented. If you want help, you shouldn't make it hard on us to evaluate your code. Go back to your original post and edit the code so that is has proper indentation. What you posted sucks. Fix it and you might actually get some useful feedback minus a few flames.
|

January 11th, 2010, 05:22 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 6
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
|
|
|
ok ill try im sorry im new to this
|

January 11th, 2010, 05:26 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 6
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
|
|
|
is that better i only formatted the text file part so far?
|

January 12th, 2010, 12:28 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
It should look like this:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
main()
{
int i;
float price;
char item[10];
int stock;
FILE * fp =fopen("price list.txt","w+");
if ( !fp )
{
printf("the file could not be found, create a file\n");
getchar();
return 1;
}
printf("Input prices\n");
for ( i=1;i<=3;i++ )
{
fscanf(stdin,"%s %f %d",item, &price, &stock);
fprintf(fp,"%s %f %d\n",item, price, stock);
}
fclose (fp);
fprintf(stdout,"\n");
fp=fopen("price list.txt","w+");
printf("Item price stock\n");
for ( i=1;i<=3;i++ )
{
fscanf(fp,"%s %f %d\n",item, &price, &stock);
fprintf(stdout,"%s %f %d\n",item, price, stock);
}
fclose(fp);
system("PAUSE");
}
Note that we do not need to see those huge blocks of code that are commented out. They are not relevant to your current issues.
|

January 12th, 2010, 05:53 PM
|
|
Registered User
|
|
Join Date: Jan 2010
Posts: 6
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
|
|
|
oh ok thanks i will know next time
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|