0

I'll make this short. I really want to learn from the answer. I am not here to make anyone code for me, so you choose if you want to learn me how to solve this problem or simply write the whole code.

I am trying to make a script which can read a CSV with this pattern: DATE,TEXT,EXPENSE or INCOME,BALANCE,STATUS,DENIED. Example: 11.01.2011,Grocery shop, -200, 700, Done, No.

I want the output to be the sum of all the expenses and income and second output what the balance is. I would like this info to be stored somewhere in the CSV file, so when I open it with excel it's there.

If you are able to explain what each line does it would be great for me so I can learn as a coding noob, but if not that's all good.

If this question is already answered I am so sorry. I have tried for a couple hours to find a answer, but I have only gotten some code that I don't know how to modify to what I want to do.

  • 1
    write some code, when you get stuck post a specifc question. Look here for some ideas https://stackoverflow.com/questions/12911299/read-csv-file-in-c – pm100 Feb 13 '22 at 19:38

1 Answers1

0

For any problem like this, I prefer to do it as a loop, structured in several parts:

  1. read lines of text
  2. when there are no more lines, we're done
  3. for each line, break it into a number of fields
  4. finally, do something interesting with the fields

The basic C function for step 1 is the fgets function. It's customery to bundle step 1 and step 2 together in the header of a while loop:

char linebuf[100];
while(fgets(linebuf, sizeof(linebuf), infp) != NULL) {
    step 3; step 4;
}

Now, in the body of the loop, we have a line of text linebuf we've just read, and it's time to break it up into fields, in this case by searching for comma characters as delimiters. One way to do this sort of thing is using the library function strtok. Another is described in this chapter of some programming notes.

For files with columns of data, I like to store the broken-out fields in an array of pointers:

char *fields[MAXCOLS];

So then, you can use ordinary string operations to do interesting things with the columns (remembering that arrays in C are 0-based).

For example, to see if column 3 is the word "yes" or something else:

if(strcmp(fields[2], "yes") == 0) {
     /* column 3 was "yes" */ ;
} else {
     /* column 3 was something else */ ;
}

If column 5 was an amount, convert it to a number so you can do something with it:

double amount, running_total;

/* ... */

amount = atof(4);
running_total += amount;

(But beware: types float or double are not always good for dealing with monetary amounts, due to roundoff issues.)

Steve Summit
  • 38,132
  • 7
  • 61
  • 86
  • Steve Summit can I ask you how it is possible to stop an array from outputting the rest of the array if what's in the CSV does not fill the array. Right now mine prints the CSV file then Wм|/к|/л|/|9||B||0|||| ||/|/|||/|||H|W|*o |p_|0|/||`||/| u@@ because the array is too big, but I can't make it smaller since I don't know how big the CSV file is. Maybe it is possible to make the array the size of the csv file. – FinancialCode Feb 13 '22 at 21:06
  • You don't have to adjust the size of the array. Just declare another "count" variable, and use it to keep track of how many values (how many columns' worth of data) you've stored in the array this time. – Steve Summit Feb 14 '22 at 00:17
  • How can I declare FILE *variable = fopen("file.csv", "r") outside of a loop, then use the variable inside of the loop? – FinancialCode Feb 14 '22 at 14:50
  • @FinancialCode You can totally declare `FILE *infp = fopen("file.csv", "r")` outside the loop, and then use `infp` to read from inside the loop. – Steve Summit Feb 14 '22 at 16:23
  • Thank you so much!. May I ask you how I can fix atoi/atof being a little unprecise. On 526 lines of CSV numbers it missed with 50. Is there a way to fix this? Right now this is what I found and use: chef = roundf(atof(lol10) * 100) / 100 + roundf(chef * 100) / 100; and the variable lol10 is declared like this: char* lol10 = strtok(NULL, "\";"); – FinancialCode Feb 14 '22 at 17:46
  • @FinancialCode `atof` is not imprecise. But floating-point numbers (both `float` and `double`) use a *binary* representation internally which leads to anomalies if you pay too-close attention to the corresponding — but usually not precisely corresponding — decimal approximations. If you converted a string like 123.456 to type `float` using `atof`, then printed it back out using `%f`, and observed slight differences — that's perfectly normal. There's not necessarily a way to "fix" it, because it is, arguably, not even wrong. Switching from `float` to `double` helps a lot, though. – Steve Summit Feb 14 '22 at 17:57
  • See [this question](https://stackoverflow.com/questions/588004) for more on this, also [this question](https://stackoverflow.com/questions/21895756). – Steve Summit Feb 14 '22 at 17:57