2Dimensional arrays..read from txt file PROBLEM! :O

This is a discussion on 2Dimensional arrays..read from txt file PROBLEM! :O within the C Programming forums, part of the General Programming Boards category; Hey everyone, so I have this following code, and what I'm supposed to do is this (assignment copied and pasted ...

2Dimensional arrays..read from txt file PROBLEM! :O

Hey everyone, so I have this following code, and what I'm supposed to do is this (assignment copied and pasted below):

Code:

The following table shows the total sales for salespeople of the ABC Manufacturing Company.
Salesperson Week 1 Week 2 Week 3 Week 4
=============== ====== ====== ====== ======
Nguyen, Michael 25 30 45 20
Johnson, Mary 32 30 33 29
...
The price of the product being sold is $1985.95 (define it as a constant). Write a program that permits the input of the data, up to 25 salespersons and up to 6 weeks, prints a replica of the original table to the screen, and writes to a file a table showing the dollar value of sales for each individual during each week along with his or her total sales. Also it prints the total sales, the lowest and the highest value for each week, and the total sales for the company.

1) How do I skip the first line of the table, in this case, the number 16 at the very top? that number just indicates the number of people there are..I'm supposed to delete that and it's not supposed to be read even thought it's in the txt file. So how do i skip that line?

2) How do I read in 2 .txt files at the same time? When i read in the 2 txt files (names.txt and sales.txt) the output should look like in the assignment above.

see how the two tables are nicely alligned? the names.txt
is on the left and the sales.txt is on the right?
How do i input both files like that and make them alligned to
each other?

regarding the first question, it's easier to work with fgets function, it reads entire line instead of reading character by character. it will save you time and headache, in my opinion. in the example below, i used 'mem', a pointer to a heap memory address, to store the line, but you can use an array of chars (just don't forget the '&' sign when using the fgets). now you can check if the ascii value of the first character in the line represents a number.

the second '...' is just copy-paste from where i stopped in your code. so, what i did above was using strncpy to copy what i got in `mem` into the `array` (see how short it is).
i haven't compiled it nor run it, but it seems fine.

OMG thanks so much for clearing that out! and actually, I changed up the parameters a bit, so now there's only one parameter, and I changed the way I open my file, similar to the pseudocode you provided. Somehow, the program runs and then it says "homework1.exe stopped working" and basically it crashed...why?!

You never check of fopen succeeded. If fp is NULL, you need to exit the getNames function before you do anything else. Otherwise, fscanf is trying to use fp, which is NULL, and it can't access the members of fp.

It makes sense that getNames would only use the names file. But it would also be silly if you wrote a getSales function, that behaved exactly like getNames, except for opened a different file. That's a bunch of repeated code, meaning twice the chance you make a mistake, twice the code to debug and maintain. If you are doing the same process for reading names and sales files, a better solution would be to go back to taking a filename as a parameter to your function:

Ohh ok! I don't think I'm gonna make one function do the same 2 things, cause my teacher prefers us to write 2 separate functions, but thanks for letting me know, i'll use it in the future! so I just fixed that part and added the NULL part, but the program still crashes!!! what am i doing wrong?