Thread: Beginner - text I/O

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Beginner - text I/O

    Okay, beginning programmer here. I need to write a program that reads an inventory file, and creates an inventory report. Specifically I am calculating how much of the item needs to be ordered(order amount). My program compiles, produces the report, but instead of creating another column with the "order amount", it calculates into the quantity column.
    Any help would be greatly appreciated. Thanks,
    -Mike
    Code:
    #include <stdafx.h>
    
    int getInventory(FILE* spIN, int* partNum, int* price, int* qty, int*reorder, int* minOrder);
    
    int writeReport	(FILE* spOUT,int partNum, int price, int qty, int reorder, int minOrder, int* orderAmt);
    
    void calcAmount (int partNum, int price, int* qty, int reorder, int minOrder, int* orderAmt);
    
    int main(void)
    {
    	FILE* spIN;
    	FILE* spOUT;
    
    	int partNum;
    	int price;
    	int qty;
    	int reorder;
    	int minOrder;
    
    	int orderAmt;
    
    	printf("Begin inventory reporting\n");
    	if (!(spIN = fopen ("inventory.txt", "r")))
    		{
    		printf("Error opening inventory file\n");
    		return 100;
    		}
    	if (!(spOUT = fopen ("report.txt", "w")))
    		{
    		printf("Error opening report file\n");
    		return 102;
    		}
    
    	while(getInventory
       		 (spIN, &partNum, &price, &qty, &reorder, &minOrder))
    		{
    		calcAmount (partNum, price, &qty, reorder, minOrder, &orderAmt);
    		writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
    		}
    	fclose (spIN);
    	fclose (spOUT);
    
    	printf("End of inventory reporting\n");
    	return 0;
    }
    int getInventory (FILE* spIN, int* partNum, int* price, int* qty, int* reorder, int* minOrder)
    {
    	int ioResult;
    
    	ioResult = fscanf(spIN, "%4d%4d%2d%2d%2d", partNum, price, qty, reorder, minOrder);
    	if (ioResult == EOF)
    		return 0;
    	else if (ioResult != 5)
    		{
    		printf("Error reading data\n");
    		return 0;
    		}
    	else
    		return 1;
    }
    
    void calcAmount (int partNum, int price, int* qty, int reorder, int minOrder, int* orderAmt)
    {
    	*orderAmt = ((reorder + minOrder) - *qty);
    	if (*qty < reorder)
    		*orderAmt = *orderAmt;
    	else 
    		*orderAmt = 0;
    		return;
    }
    
    int writeReport (FILE* spOUT, int partNum, int price, int qty, int reorder, int minOrder, int orderAmt)
    {
    	fprintf(spOUT," %d %d %d %d %d %d\n", partNum, price, qty, reorder, minOrder, orderAmt);
    
    	return 0;
    }
    
    INPUT FILE
    0123 01.23 23 20 20
    0234 02.34 34 50 25
    3456 34.56 56 50 10
    4567 45.67 07 10 05
    5678 06.78 75 75 25
    
    OUTPUT FILE
    0123 1.230000 23 20 20
    0234 2.340000 41 50 25
    3456 34.560001 56 50 10
    4567 45.669998 8 10 5
    5678 6.780000 75 75 25

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Use %2.2f to print a floating-point number with two digits, to the second decimal place.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Babkockdood: Am I supposed to add the %2.2f to my "getInventory" or "writeReport" function? I tried both and it didn't seem to have any effect.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by MGhiotti View Post
    Babkockdood: Am I supposed to add the %2.2f to my "getInventory" or "writeReport" function? I tried both and it didn't seem to have any effect.
    That seems to be why you're getting extra zeros in the output file, it could be something else. You could add an if statement to print a preceding zero if a number is below ten.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    I think it's something else. I changed both functions to %2.2f, no change. So I changed all my "int price" to "float price", still no change. I have no clue why it's doing that. But I'm also a beginner and debugging is a little trying anyhow. Do you think that has any effect on why it won't print the extra column in my report.txt?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is just a quick markup... I've probably got at least one error in there but this should point you in the right direction....

    A couple of suggestions ...

    stdafx is not a C header... it's C++ ... use stdio.h for C.

    In several case you are passing unnecessary parameters into functions. Way too much can go wrong when you do that.

    Try not to pass pointers around willy-nilly... again, way too much can go wrong.

    In your input file price is a float value (i.e. it has a decimal point in it) reading that as an int will surely lead to file read problems.


    Code:
    #include <stdio.h>
    
    int getInventory(FILE* spIN, int* partNum, float* price, int* qty, int*reorder, int* minOrder);
    
    int writeReport(FILE* spOUT,int partNum, float  price, int qty, int reorder, int minOrder, int orderAmt);
    
    int calcAmount (int qty, int reorder, int minOrder);
    
    int main(void)
    {
    	FILE* spIN;
    	FILE* spOUT;
    
    	int partNum;
    	float price;
    	int qty;
    	int reorder;
    	int minOrder;
    
    	int orderAmt;
    
    	printf("Begin inventory reporting\n");
    	if (!(spIN = fopen ("inventory.txt", "r")))
    		{
    		printf("Error opening inventory file\n");
    		return 100;
    		}
    	if (!(spOUT = fopen ("report.txt", "w")))
    		{
    		printf("Error opening report file\n");
    		return 102;
    		}
    
    	while(getInventory
       		 (spIN, &partNum, &price, &qty, &reorder, &minOrder))
    		{
    		orderAmt = calcAmount (qty, reorder, minOrder);
    		writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
    		}
    	fclose (spIN);
    	fclose (spOUT);
    
    	printf("End of inventory reporting\n");
    	return 0;
    }
    int getInventory (FILE* spIN, int* partNum, float* price, int* qty, int* reorder, int* minOrder)
    {
    	int ioResult;
    
    	ioResult = fscanf(spIN, "%d%f%d%d%d", partNum, price, qty, reorder, minOrder);
    	if (ioResult == EOF)
    		return 0;
    	else if (ioResult != 5)
    		{
    		printf("Error reading data\n");
    		return 0;
    		}
    	else
    		return 1;
    }
    
    int calcAmount (int qty, int reorder, int minOrder)
         { if (qty < reorder)
              return ((reorder + minOrder) - qty);
            return 0; }
    
    int writeReport (FILE* spOUT, int partNum, int price, int qty, int reorder, int minOrder, int orderAmt)
    {
    	fprintf(spOUT," %4d %0.2f %2d %2d %2d %2d\n", partNum, price, qty, reorder, minOrder, orderAmt);
    
    	return 0;
    }
    Code:
    INPUT FILE
    0123 01.23 23 20 20
    0234 02.34 34 50 25
    3456 34.56 56 50 10
    4567 45.67 07 10 05
    5678 06.78 75 75 25
    
    OUTPUT FILE
    0123 1.230000 23 20 20
    0234 2.340000 41 50 25
    3456 34.560001 56 50 10
    4567 45.669998 8 10 5
    5678 6.780000 75 75 25
    Last edited by CommonTater; 04-06-2011 at 02:37 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your fscanf() format strings should not include numbers - that's for printf() formatting.

    Code:
    fscanf(spIN, "%4d%4d%2d%2d%2d", partNum, price, qty, reorder, minOrder);
    I highlighted a few of them, but all those numbers in the fscanf() format string, should be removed. Why the %d for price? I though it was a float?
    Last edited by Adak; 04-06-2011 at 02:39 PM.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Thanks for the replies very helpful stuff.
    CommonTater: I'm still having the exact same output, I changed everything you highlighted. I'm going to study your changes more thoroughly to see what the problem could be.

    Adak:Thanks for pointing out the error in my fscanf format string, also I have changed all of the price to float from int.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This part leaves me chuckling a bit.

    Code:
    void calcAmount (int partNum, int price, int* qty, int reorder, int minOrder, int* orderAmt)
    {
    	*orderAmt = ((reorder + minOrder) - *qty);
    	if (*qty < reorder)
    		*orderAmt = *orderAmt; 
    	else 
    		*orderAmt = 0;
    		return;
    }
    If *qty is less than reorder, then *orderAmt should be assigned the minOrder quantity.
    Last edited by Adak; 04-06-2011 at 04:25 PM.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Yeah it felt awkward when I was typing it. Tater showed me a much better way.
    Code:
    while(getInventory
       		 (spIN, &partNum, &price, &qty, &reorder, &minOrder))
    		{
    		orderAmt = calcAmount (qty, reorder, minOrder);
    		writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
    		}
    blah blah blah........................
    int calcAmount (int qty, int reorder, int minOrder)
         { if (qty < reorder)
              return ((reorder + minOrder) - qty);
            return 0; }
    It works I get the correct numbers in my output file now.

    Here's my updated code:
    Code:
    #include <stdafx.h>
    #include <stdio.h>
    int getInventory(FILE* spIN, int* partNum, float* price, int* qty, int*reorder, int* minOrder);
    
    int writeReport	(FILE* spOUT,int partNum, float price, int qty, int reorder, int minOrder, int orderAmt);
    
    int calcAmount (int qty, int reorder, int minOrder);
    
    int main(void)
    {
    	FILE* spIN;
    	FILE* spOUT;
    
    	int partNum;
    	float price;
    	int qty;
    	int reorder;
    	int minOrder;
    
    	int orderAmt;
    
    	printf("Begin inventory reporting\n");
    	if (!(spIN = fopen ("inventory.txt", "r")))
    		{
    		printf("Error opening inventory file\n");
    		return 100;
    		}
    	if (!(spOUT = fopen ("report.txt", "w")))
    		{
    		printf("Error opening report file\n");
    		return 102;
    		}
    
    	while(getInventory
       		 (spIN, &partNum, &price, &qty, &reorder, &minOrder))
    		{
    		orderAmt = calcAmount (qty, reorder, minOrder);
    		writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
    		}
    	fclose (spIN);
    	fclose (spOUT);
    
    	printf("End of inventory reporting\n");
    	return 0;
    }
    int getInventory (FILE* spIN, int* partNum, float* price, int* qty, int* reorder, int* minOrder)
    {
    	int ioResult;
    
    	ioResult = fscanf(spIN, "%d%f%d%d%d", partNum, price, qty, reorder, minOrder);
    	if (ioResult == EOF)
    		return 0;
    	else if (ioResult != 5)
    		{
    		printf("Error reading data\n");
    		return 0;
    		}
    	else
    		return 1;
    }
    
    int calcAmount (int qty, int reorder, int minOrder)
    {
    	if (qty < reorder)
    		return ((reorder + minOrder) - qty);
    		return 0;
    }
    
    int writeReport (FILE* spOUT, int partNum, float price, int qty, int reorder, int minOrder, int orderAmt)
    {
    	fprintf(spOUT," %4d %0.2f %2d %2d %2d %2d\n", partNum, price, qty, reorder, minOrder, orderAmt);
    
    	return 0;
    }
    Gives me the proper numbers, just messy.
    Code:
      123 1.23 23 20 20  0
      234 2.34 34 50 25 41
     3456 34.56 56 50 10  0
     4567 45.67  7 10  5  8
     5678 6.78 75 75 25  0
    Might be a stupid question but how do I add headers to the output file? Would I just include them in the fprintf statement or have an entirely different one?
    Appreciate all the help guys.
    -Mike
    Last edited by MGhiotti; 04-06-2011 at 04:38 PM. Reason: keeps double posting

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You will need a printf() above the loop that only gets executed once.

    If you want really tidy columns try this...

    Code:
    int writeReport (FILE* spOUT, int partNum, float price, int qty, int reorder, int minOrder, int orderAmt)
    {
    	fprintf(spOUT," %4d\t%0.2f\t%2d\t%2d\t%2d\t%2d\n", partNum, price, qty, reorder, minOrder, orderAmt);
    
    	return 0;
    }
    The \t columnates on 8 character boundaries... (or at least that's the default).
    Last edited by CommonTater; 04-06-2011 at 05:31 PM.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    I actually wound up doing it like this before I saw your reply. Thanks for your help Tater, you've been awesome.
    Code:
    fprintf(spOUT, " %04d %05.2f %02d %02d %02d %02d\n", partNum, price, qty, reorder, minOrder, orderAmt);
    Kind of confused where to put it. You said before the loop. I put it in the loop and it repeated with it originally. I also tried sticking it in main, which didn't work out. Do I need to add something extra, sorry if this is ridiculously obvious.
    -Mike

    Okay I think I understand what you meant now.
    If I insert fprintf before the while loop starts it prints out above the numbers which is what I was looking for.
    Code:
    if (!(spOUT = fopen ("report.txt", "w")))
    		{
    		printf("Error opening report file\n");
    		return 102;
    		}
    		fprintf(spOUT, "Inventory Report\n");
    	while(getInventory
       		 (spIN, &partNum, &price, &qty, &reorder, &minOrder))
    		{
    		orderAmt = calcAmount (qty, reorder, minOrder);
    		writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
    		}
    Last edited by MGhiotti; 04-06-2011 at 06:48 PM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you're using tabs in your writeReport() you can also do this....

    Code:
    if (!(spOUT = fopen ("report.txt", "w")))
    		{
    		printf("Error opening report file\n");
    		return 102;
    		}
    		fprintf(spOUT, "Inventory Report\n");
                     frpintf("Part\tPrice\tQty\tReorder\tMin\tOrder\n\n");
    
    	while(getInventory
       		 (spIN, &partNum, &price, &qty, &reorder, &minOrder))
    		{
    		orderAmt = calcAmount (qty, reorder, minOrder);
    		writeReport (spOUT, partNum, price, qty, reorder, minOrder, orderAmt);
    		}

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    I actually do need to label each column so that seems like it will work out nicely, thanks I'll be trying it out.
    -Mike

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    I actually do need to label each column so that seems like it will work out nicely, thanks I'll be trying it out.
    -Mike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting Edit Control Text
    By yoonkwun in forum Windows Programming
    Replies: 2
    Last Post: 07-12-2009, 08:48 AM
  2. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM