C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2010, 02:50 PM
jazdaboss jazdaboss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 6 jazdaboss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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");
}

 

Reply With Quote
  #2  
Old January 11th, 2010, 03:09 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,477 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 35 m 10 sec
Reputation Power: 2194
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Reply With Quote
  #3  
Old January 11th, 2010, 03:11 PM
jazdaboss jazdaboss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 6 jazdaboss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old January 11th, 2010, 04:57 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
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/

Reply With Quote
  #5  
Old January 11th, 2010, 04:58 PM
jazdaboss jazdaboss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 6 jazdaboss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
what do you mean reformat the code?

Reply With Quote
  #6  
Old January 11th, 2010, 05:02 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,477 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 35 m 10 sec
Reputation Power: 2194
You're trying to be a cut-and-paster. That ways lies failure. Make an effort to learn what it is you're doing.

Reply With Quote
  #7  
Old January 11th, 2010, 05:04 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
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.

Reply With Quote
  #8  
Old January 11th, 2010, 05:06 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
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.

Reply With Quote
  #9  
Old January 11th, 2010, 05:22 PM
jazdaboss jazdaboss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 6 jazdaboss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
ok ill try im sorry im new to this

Reply With Quote
  #10  
Old January 11th, 2010, 05:26 PM
jazdaboss jazdaboss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 6 jazdaboss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #11  
Old January 12th, 2010, 12:28 PM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
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.

Reply With Quote
  #12  
Old January 12th, 2010, 05:53 PM
jazdaboss jazdaboss is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 6 jazdaboss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m 12 sec
Reputation Power: 0
oh ok thanks i will know next time

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Noob help with text files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap