2

I have a super basic question but I can't seem to found an answer to this. Perhaps I'm over thinking this, and if that's the case, I apologize beforehand.

My problem lies in reading an input file filled with information that'll be printed out into invoices. This is what my assignment calls for:

"Your program should read in the entire input file data before generating any invoice. This means that you may not generate the invoices on the fly – that is as you read the data."

As it were, I currently just have this line (which is my standard for reading in a file):

FILE *input = fopen("invoice1.txt","r");

My current understanding is this is just setting a pointer to the beginning of the input file, ready to be read in, BUT doesn't actually read the file at that point. Am I wrong and this actually will read the file like I need it to? How would I accomplish simply reading through the file without saving the information anywhere?

Thank you!

bravesaint
  • 129
  • 4
  • 14
  • 2
    `fopen` stands for "file open". It does not read the file. Use `fgets` to read it. And no, it is not possible to read, but not save the read data. Your assignment does not tell not to save the data. It tells not to print(or maybe parse) the data while reading. – Spikatrix Apr 24 '15 at 02:28
  • @CoolGuy that's what I had figured. How would you go about doing what they're asking for then? It's leaving me puzzled as to what exactly to do! If I can't get a general consensus, I'll simply find out **exactly** she meant by this tomorrow. – bravesaint Apr 24 '15 at 02:31
  • 1
    You must read the contents of the file and store it in memory as you go. Typically you would *parse* it into appropriate data structures as you go along, but you could also just load the raw data into memory and parse it afterward. If you truly have no idea at all how to do any of this, then throw yourself on your instructor's mercy, or start reading your textbook. – John Bollinger Apr 24 '15 at 02:35
  • @CoolGuy yes yes, that makes complete sense now!!! Thank you! I was interpreting that as reading everything *without* saving it **at all** during an initial run through. I now understand that they simply just didn't want me to read the line and immediately print it out. It just hit me as odd because that's how all of the assignments have been thus far and a note like that was never added. – bravesaint Apr 24 '15 at 02:41
  • @JohnBollinger I get what you're saying! Like I said in my OP, I was probably just over-thinking this way too much. Thanks for the reply! – bravesaint Apr 24 '15 at 02:42

3 Answers3

2

In fact, fopen() will return a FILE * that will be used later by other functions like fgets() for reading.

Here is a website with a complete example on what you can do for reading : http://www.programmingsimplified.com/c-program-read-file

Also, here's a list of functions from the stdio.h that you can use for file manipulation/reading. The functions that will interest you will mostly be those with the f prefix in front of them : http://www.cplusplus.com/reference/cstdio/

EDIT : If you look at the first link that I have showed you, the program outputs the file data every time it reads a character (note that ch should be of type int not char) :

 while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);

This seems to be exactly what your teacher doesn't want : "This means that you may not generate the invoices on the fly – that is as you read the data."

In your case, you will probably want to use a function like fgets(), which reads the whole file and dumps it into a char[] for later use : http://www.cplusplus.com/reference/cstdio/fgets/

Corb3nik
  • 1,177
  • 7
  • 26
  • Thanks! In that first example though, it's only reading the file in the last `while` loop, which in turn, is printing the data to the screen, am I correct? – bravesaint Apr 24 '15 at 02:37
  • fgetc() checks the character at the current position. In the last while loop, it reads each character and prints them out : as opposed to reading the whole file, and printing it out afterwards. – Corb3nik Apr 24 '15 at 02:39
  • @chux Would you mind explaining what do you mean? I do not understand your comment. Thank you – Corb3nik Apr 24 '15 at 02:39
  • 1
    @Cubia , chux tells you to add "`ch` should be an `int`, not a `char`" in your answer. – Spikatrix Apr 24 '15 at 02:48
  • @Cubia thank you for the further explanations! Completely understand what she was getting at now! Thank you! – bravesaint Apr 24 '15 at 02:49
2

"Your program should read in the entire input file data before generating any invoice. This means that you may not generate the invoices on the fly – that is as you read the data."

My first thought after reading your assignment was that your overall task is to generate an invoice in some steps:

  1. Open your file as you did.
  2. Read ALL the data into some kind of variables.
  3. Generate invoice.
  4. Store it somewhere.

This quotation specifies that you CAN'T start creating your invoice when you are not done with loading your data from file. The conclusion is that you have to store all the data somewhere in your program (e.g. identify name, surname as char arrays) and use your variables to generate new invoice (this would be done in a separate function, like readData() ). When you have all the data stored somewhere as variables, you are supposed to use them as parameters for a function generating the invoice (like invoice generateInvoice(char[20] name, char[20] surname, ...) ).

irchris102
  • 108
  • 10
2

My current understanding is this is just setting a pointer to the beginning of the input file, ready to be read in, BUT doesn't actually read the file at that point.

That is correct.

Am I wrong and this actually will read the file like I need it to?

No, you are not wrong.

How would I accomplish simply reading through the file without saving the information anywhere?

I suspect your teacher wants you to read the entire contents of the file into a buffer and process the buffer instead of reading the contents of the file line by line (or record by record) and process them.

This answer contains code that shows how to read the entire contents of a file.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you for the reply! Checking out that answer now! And thank you for acknowledging my line of thinking - it's always super helpful to make sure I'm "thinking like a programmer" as it were. :) – bravesaint Apr 24 '15 at 02:46