i'm asking for homework help. i've come to my wits end trying to figure this out and it's due midnight tommorrow est.

i'm supposed to write a program to read an inventory file and create the inventory report ( i did that with the given data minus the 5 column headings like it told me to). along with the 5 columns, the report is also going to include the order amount ( calculated as the sum of the reorder point and the minimum order less the quantity on hand). i am to provide a report heading and ending message ( done) and i'm to print the part number with leading zeros. my teacher told us to save the inventory txt in the root directory but my computer wont allow me to because i have vista. he also said we could save it where our project solution is saved (?!!!??). i've only done the first part of my code because i don't think i'm going about this the right way. i've searched the web and i don't think i want to read a whole line but i'm not sure and there isn't an example in my book that is close to this project ( to my limited understanding).

here is my code:

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"


void ordAmt( int reOrder, int minOrd, int quant);

int _tmain(void)
{
  // local declarations
	FILE*spInventRep;
	 int  partn;
	 int  price;
	 int  quant;
	 int  reOrder;
	 int  minOrd;
	 int  ordAmt;

  //Statements
	spInventRep = fopen("C:\Users\Jenniy\Desktop\ch 7\inventory09.txt", "r");
	
	if ((spInventRep = fopen("inventory09.txt", "r")) ==NULL)
	{
  printf("Error opening file\n"); 
		} // if

	printf("\t\t\t Inventory Report\n\n");

	printf("Part Number");
		fscanf(spInventRep, "%4f", &partn);

	printf("\t Price");
	do
	{
		fscanf(spInventRep, "%5.2f", &price);
	if (partn= 0123) [B]\\ these are given in the inventory report [/B]
		&price = 1.23; [B]\\ as are these, but there are suposed to be loops according to the examples in the book...U.U[/B]
	if (partn= 0234)
		&price= 2.34;
	if (partn= 3456)
		&price= 34.56;
	if (partn= 4567)
		&price= 45.67;
	if (partn= 5678)
		&price= 6.78;
	}while (partn != EOF);

	printf("\t Quantity on Hand");
		fscanf(spInventRep, "%3f", &quant);

	printf("\t Reorder Point");
		fscanf(spInventRep, "%2f", &reOrder);

	printf("\t Minimum Order");
		fscanf(spInventRep, "%2f", &minOrd);

	printf("\t Order Amount");
		fscanf(spInventRep, "%3f", &ordAmt); 

	printf("\n\n\n\t\t\t End of Report");

	return 0;
}

Thank you anyone for helping...

line 20: you need to use two '\' folder separator characters so that the compiler doesn't treat them as escape characters spInventRep = fopen("C:\\Users\\Jenniy\\Desktop\\ch 7\\inventory09.txt", "r"); line 22: you can not use the same FILE pointer for both input and output files -- use two different names.

line 36: use the == boolean operator, not the = assignment operator

line 37: remove the & pointer operator -- just do a normal assignment here price=1.23; Same thing on line 39-45.

It would help if you would post a few lines of the input data file.


line 22: you can not use the same FILE pointer for both input and output files -- use two different names.

QUOTE]
I thought that if you are only reading the file and not having to write you only needed one file. Thank you that helped but i'm still getting an error when i ran the debug. the warning looks like this.
--------------------------------------------------
Debug assertion failed

program:...
file:fscan.c
line:52

expression: (stream != NULL)
---------------------------------------------------

here is the first data line i separated the collumns by a space.

0123 1.23 25 20 15

here's another question. when someone uses the program, they can only call up one item of data at a time or can they call up the entire inventory report? if they can call up the entire inventory report how do you make the program have collumns? the assignment said to include captions for each collumn.

Let's see if I can help you to clarify what you are doing.
When you want to work in a file, you have to open it regardless it is for reading or for writing in it.
The path where that file lives needs to be specified in absolute path if it is not living where the program executable is.
In windows you need to scape the path delimiters, since (\) is interpreted as an special character.
So

"C:\Users\Jenniy\Desktop\ch 7\inventory09.txt"

becomes

"C:\\Users\\Jenniy\\Desktop\\ch 7\\inventory09.txt"

Let's look at this.

spInventRep = fopen("C:\Users\Jenniy\Desktop\ch 7\inventory09.txt", "r");
	
	if ((spInventRep = fopen("inventory09.txt", "r")) ==NULL)

If "inventory09.txt" and "C:\Users\Jenniy\Desktop\ch 7\inventory09.txt" are the same file, you are trying to open it twice.
The first time you don't check for error, the second time you check for error.
One time it is all you need.

If the file is NULL, then why are you still using it afterwords? Perhaps you should have one function to load, and one to save.

You aren't closing the file pointer after you're finished.

A little heavy commented example, perhaps?

#include <stdio.h>
#include <stdlib.h> /* for the function exit() */

int main(void)
{
	FILE *input_file; /* handle for file to read */
	FILE *output_file; /* handle for file to write to */
	char buffer[BUFSIZ]; /* guaranteed to be at least 256 characters buffer */
	char *path_to_read = "C:\\overhere\\warm\\warmer\\hotfile.txt";
	char *path_to_write = "C:\\overhere\\warm\\warmer\\coolfile.txt";

	/* trying to read file */
	input_file = fopen(path_to_read, "r");
	/* did it open? */
	if (input_file == NULL) {
		/* no joy, file failed to open, issue error */
		fprintf(stderr, "%s failed to open\n", path_to_read);
		/* nothing else to do but exit the program */
		exit(1);
	}

	/* trying to open file to write */
	output_file = fopen(path_to_write, "w");
	if (output_file == NULL) {
		fprintf(stderr, "Weird error creating %s\n", path_to_write);
		/* oh well, we can't write to file, exiting */
		exit(1);
	}

	/* read line by line from input_file */
	while (fgets(buffer, sizeof buffer, input_file) != NULL) {
		/* here I chose just to copy line for line
		 * but you can parse the string read by fgets and
		 * extract what you want with sscanf or any other
		 * function and format at your heart's content */
		fprintf(output_file, buffer);
	}

	/* we're done close files */
	fclose(output_file);
	fclose(input_file);
	return 0;
}

That's a generic way of opening and working with files.

This article has been dead for over six months. Start a new discussion instead.