Thread: int varriable print to file

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    28

    int varriable print to file

    Hello,
    New problem.

    maybe it's stupid but I'm stuck.

    I want to write a long int varriable into a file, do I need to transform it to binary take the lower 8 bits and using fputc print and remove them
    every time for the entire number? or is there a better way?

    I've searched the board but all I find are functions to print chars,
    strings and arrays when what I need is a simple int var.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You can use fprintf to print to a file.
    Code:
    FILE *infile = fopen(FileName, "w");
    fprintf(infile, "%l", LongVar);
    fclose(infile);
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fprintf(infile, "%l", LongVar);
    %l isn't a format specifier for the printf family, I think you meant %ld.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    28
    I wrote part of a bigger program and it has a few problems:

    1. double prints the menu.

    2. where the user has to print y to continue it prints the Q but
    automaticaly continues.

    I don't know why.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void New_Item(int);
    
    void main(){
    	char date[9];
    	FILE *it;
    	FILE *his;
    	char choice;
    	char user_name[20];
    	
    	printf("please enter user name\n");
    	scanf("%s",&user_name);
    	printf("please enter date\n");
    	scanf("%s",&date);
    		
    	while(choice!='9')
    	{
    		printf("please enter an option\n");
    		printf("%d to enter new item\n",1);
    		printf("%d to delete item\n",2);
    		printf("%d to add to stock\n",3);
    		printf("%d to remove from stock\n",4);
    		printf("%d to update comments\n",5);
    		printf("%d to item inquire\n",6);
    		printf("%d for report\n",7);
    		printf("%d to end\n",8);
    		printf("%d to logout\n",9);
    		scanf("%c",&choice);
    		switch(choice){
    
    		case '1':
    			New_Item(1);
    			break;
    		}
    	}
    }
    
    void New_Item(int){
    char ans;
    long int itemid=0,qntty=0;
    char date[9],item_name[20],comm[40];
    FILE *it;
    	if((it=fopen("Items","r+"))==NULL)
    	{
    		printf("you want to start a new file\n");
    		printf("y or n\n");
    		scanf("%c",&ans);
    		if(ans=='n' || ans=='N')
    			return;
    	}
    	
    	for( ; ; )
    	{
    		printf("enter item's id, max 999999999\n");
    		if(scanf("%ld",itemid)==1)
    		{
    			if(itemid>0 && itemid<=999999999)
    			{
    				printf("enter item's name max %d chars\n",20);
    				fgets(item_name,20,stdin);
    				printf("enter item's quantity \n");
    			
    				if(scanf("%ld",qntty)==1)
    				{
    					if(qntty>0 && qntty<=1000000)
    					{
    						printf("enter comments\n");
    						fgets(comm,40,stdin);
    					}
    					else
    					{
    						printf("item's quantity is out of range\n");
    						printf("quantity can be between %d and %d\n",1,1000000);
    					}
    				}
    				else
    					printf("that was not a number\n");
    			}
    			else
    			{
    				printf("item's id is out of range\n");
    				printf("id can be between %d and %d\n",1,999999999);
    			}
    		}
    		else
    			printf("that was not a number\n");
    		while(getchar()!='\n');
    	}
    
    	fprintf(it,"%ld ",itemid);
    	fputs(item_name,it);
    	fputc(' ',it);
    	fprintf(it,"%ld ",qntty);
    	fputs(comm,it);
    	fclose(it);
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First spot:
    >void New_Item(int){
    your haven't named the variable.

    >Unreachable code.
    The for (;;) loop in the New_Item() function never ends, there's no break, return etc.

    It double prints the menu because theres a new line character left in the buffer from this scanf()
    >scanf("%s", &date);
    and when it reaches this scanf()
    >scanf("%c", &choice);
    the newline is read in again.

    >if(scanf("%ld",itemid)==1)
    scanf() news a pointer for its second arg. Use &itemid

    >printf("quantity can be between %d and %d\n", 1, 1000000);
    %d is for int's which are limited in size (as per one of our other conversations). The number 1000000 is too big. Use %ld.
    Last edited by Hammer; 05-14-2002 at 12:20 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest
    Originally posted by Hammer
    First spot:
    >void New_Item(int){
    your haven't named the variable.

    >Unreachable code.
    The for (; loop in the New_Item() function never ends, there's no break, return etc.

    It double prints the menu because theres a new line character left in the buffer from this scanf()
    >scanf("%s", &date);
    and when it reaches this scanf()
    >scanf("%c", &choice);
    the newline is read in again.
    Is this better?

    > for(;getchar()!='\n'

    > void New_Item(int num)

    1. the thing is, I'm not using any variable frm "main" I only used it
    to call the function. is that o.k?

    2. I still don't understand why it double prints the menu.

    I'm sorry but I'm still at the begining of c prog.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    28
    oops....

    forgot to register before.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    After this:
    >scanf("%s", &date);
    put this:
    >while (getchar() != '\n');
    This will eat up the extra byte from the input buffer.

    (and yes, use the while not for version)

    Also
    >void main(void)
    No, use int main(void)

    >char choice;
    >while (choice != '9')
    You didn't initialise choice before you tested it for a specific value. Better to set it to something first. Eg:
    >char choice = ' ';

    Did you fix your infinite loop?
    Last edited by Hammer; 05-14-2002 at 01:00 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    28
    Originally posted by Hammer
    After this:
    >scanf("%s", &date);
    put this:
    >while (getchar() != '\n');
    This will eat up the extra byte from the input buffer.

    (and yes, use the while not for version)

    Also
    >void main(void)
    No, use int main(void)

    >char choice;
    >while (choice != '9')
    You didn't initialise choice before you tested it for a specific value. Better to set it to something first. Eg:
    >char choice = ' ';

    Did you fix your infinite loop?
    1. my loop now looks like this : "for(;getchar()!='\n'; )".

    2. I initialised "choice" and used "while (getchar() != '\n');" after
    "scanf("%s", &date);".

    now it stoped double printing the menu.

    Thank You.
    --------------------------------------------------------
    I hope you won't mind if I ask another Q?...

    When I try to enter the item id no. I get the following error message:

    The instruction at "0x00406188" referenced
    memory "0x00000000". The memory could not be "written".

    What does it mean?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >The instruction at "0x00406188" referenced
    >memory "0x00000000". The memory could not be "written".
    Do you mean this line:
    >if (scanf("%ld", itemid) == 1)
    If so, you've forgotten the &. Make it &itemid.

    >my loop now looks like this : "for(;getchar()!='\n'; )".
    It shouldn't. The outer statement
    >for ( ;; )
    is correct, this does create an infinite loop, you just need to force a break-out when you get the data you want. (Sorry, maybe I confused you earlier). I'd guess that would be after you've asked for comments:
    >fgets(comm, 40, stdin);
    So, I'd suggest adding a break; statement after that line (I am presuming that once the user has entered comments, you want to leave the for loop and write the data to the file).

    This bit
    >while (getchar() != '\n');
    is used to clear the input buffer and I believe should be left as the last thing in the for loop. That way, the input buffer is cleared before going back to the top and asking for the itemid again.

    If you get stuck, post that function again with you latest changes, an I'll have another look.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    28
    [i]>If you get stuck, post that function again with you latest changes, an I'll have another look.[/B]
    I did the changes and it just goes crazy.

    I think it's best just to post the function.

    Hopfully after this I will be able to do the remaining 5 functions
    all by myself.
    (I thought the algoritems were the hard part. I was sooo wrong.)

    By the way we HAVE to use " void main" I know "int main" is better but I'm not a professor yet.


    Code:
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void New_Item(int);
    
    void main(){
    	char date[9];
    	FILE *it;
    	FILE *his;
    	char choice=' ';
    	char user_name[20];
    	
    	printf("please enter user name\n");
    	scanf("%s",&user_name);
    	printf("please enter date\n");
    	scanf("%s",&date);
    	while (getchar() != '\n'); 
    		
    	while(choice!='9')
    	{
    		printf("please enter an option\n");
    		printf("%d to enter new item\n",1);
    		printf("%d to delete item\n",2);
    		printf("%d to add to stock\n",3);
    		printf("%d to remove from stock\n",4);
    		printf("%d to update comments\n",5);
    		printf("%d to item inquire\n",6);
    		printf("%d for report\n",7);
    		printf("%d to end\n",8);
    		printf("%d to logout\n",9);
    		scanf("%c",&choice);
    	 
    		switch(choice){
    
    		case '1':
    		
    			New_Item(1);
    
    			break;
    		}
    	}
    }
    
    
    void New_Item(int num){
    char ans;
    long int itemid=0,qntty=0;
    char date[9],item_name[20],comm[40];
    FILE *it;
    FILE *his;
    
    	if((it=fopen("Items","r+"))==NULL)
    	{
    		printf("you want to start a new file\n");
    		printf("y or n\n");
    		scanf("%c",&ans);
    		if(ans=='n' || ans=='N')
    			return;
    	}
    	for(;;)
    	{
    		printf("enter item's id, max 999999999\n");
    		if(scanf("%ld",&itemid)==1)
    		{
    			if(itemid>0 && itemid<=999999999)
    			{
    				printf("enter item's name max %d chars\n",20);
    				fgets(item_name,20,stdin);
    				printf("enter item's quantity \n");
    			
    				if(scanf("%ld",&qntty)==1)
    				{
    					if(qntty>0 && qntty<=1000000)
    					{
    						printf("enter comments\n");
    						fgets(comm,40,stdin);
    						break;
    					}
    					else
    					{
    						printf("item's quantity is out of range\n");
    						printf("quantity can be between %d and %d\n",1,1000000);
    					}
    				}
    				else
    					printf("that was not a number\n");
    			}
    			else
    			{
    				printf("item's id is out of range\n");
    				printf("id can be between %d and %d\n",1,999999999);
    			}
    		}
    		else
    			printf("that was not a number\n");
    		while(getchar()!='\n');
    	}
    
    	fprintf(it,"%ld ",itemid);
    	fputs(item_name,it);
    	fputc(' ',it);
    	fprintf(it,"%ld ",qntty);
    	fputs(comm,it);
    	fclose(it);
    
    	if((his=fopen("history","a"))!=NULL)
    	{
    		fprintf(his,"%d",itemid);
    		fputs(item_name,his);
    		fprintf(his,"%d",qntty);
    		fclose(his);
    	}
    }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I have added a few bits to your code, see my comments in the source below (watch for "NEW BIT"). You had done alright, it just needed the buffers flushing between each scanf() call.

    There is another problem now. The logic for determining if the file "it" is open is flawed. On my PC, I don't have a file called Items, so the fopen() fails
    >if ((it = fopen("Items", "r+")) == NULL)
    You ask if you want the user to create a new file, and if Y is entered you carry on. But you don't actually open a file in this case. You then proceed through the section getting user data, and then try to write out to an unopened file pointer. If I create the file first, then the fopen() works OK,and there progs writes out correctly.

    I'll leave that one for you to sort out for now

    Code:
    void New_Item(int num)
    {
    	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    	char		ans;
    	long int	itemid = 0, qntty = 0;
    	char		date[9], item_name[20], comm[40];
    	FILE		*it;
    	FILE		*his;
    	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    	if ((it = fopen("Items", "r+")) == NULL)
    	{
    		printf("you want to start a new file\n");
    		printf("y or n\n");
    		scanf("%c", &ans);
    		if (ans == 'n' || ans == 'N') return;
    		while (getchar() != '\n'); /* NEW BIT */
    	}
    
    	for (;;)
    	{
    		printf("enter item's id, max 999999999\n");
    		if (scanf("%ld", &itemid) == 1)
    		{
    			while (getchar() != '\n'); /* NEW BIT */
    			if (itemid > 0 && itemid <= 999999999)
    			{
    				printf("enter item's name max %d chars\n", 20);
    				fgets(item_name, 20, stdin);
    				printf("enter item's quantity \n");
    				if (scanf("%ld", &qntty) == 1)
    				{
    					while (getchar() != '\n'); /* NEW BIT */
    					if (qntty > 0 && qntty <= 1000000)
    					{
    						printf("enter comments\n");
    						fgets(comm, 40, stdin);
    						break;
    					}
    					else
    					{
    						printf("item's quantity is out of range\n");
    						printf("quantity can be between %d and %d\n", 1, 1000000);
    					}
    				}
    				else
    					printf("that was not a number\n");
    			}
    			else
    			{
    				printf("item's id is out of range\n");
    				printf("id can be between %d and %d\n", 1, 999999999);
    			}
    		}
    		else
    			printf("that was not a number\n");
    		while (getchar() != '\n');
    	}
    
    	fprintf(it, "%ld ", itemid);
    	fputs(item_name, it);
    	fputc(' ', it);
    	fprintf(it, "%ld ", qntty);
    	fputs(comm, it);
    	fclose(it);
    	if ((his = fopen("history", "a")) != NULL)
    	{
    		fprintf(his, "%d", itemid);
    		fputs(item_name, his);
    		fprintf(his, "%d", qntty);
    		fclose(his);
    	}
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    28
    sorry it took me so long to reply.
    I don't have a compiler at home.

    Thank u for the help, Hammer.

    I have attached my more extended program.
    I'm having a problem. it doesn't accept my 2nd case.

    help would be appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM