Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello. I would like to read a CSV file in C, but the program only reads the first 2 lines, after that it stops (I check this with a printf).
Imgur: The magic of the Internet[^]
Here my codes:GitHub - 420bela/help: h[^]
That file contains the actual code:excelbeolvas.c

What I have tried:

I have tried sscanf, but that code didnt work as well.
Updated 17-Nov-23 4:31am
Richard MacCutchan 17-Nov-23 6:46am    
I expect there is something wrong in your code but it is impossible to guess where. You need to use the debugger to find out what is going on when you run the code. Also note, please post the relevant parts of your code and error details here in your question. Do not expect us to download code from other sites.
420bela 17-Nov-23 6:57am    
Okey, can you suggest my a great debugger? I think you dont have to download my code, you can see in github.Excelbeolvas.c contains the import codes.
Richard MacCutchan 17-Nov-23 7:25am    
No, I am not spending time on github. This is your third time of asking this question and unless you provide the information we ask for there is nothing we can do to help. As to using a debugger, the standard one built into Visual Studio will do everything you need.
420bela 17-Nov-23 7:36am    
Sorry, but I have to use CodeBlocks.
Richard MacCutchan 17-Nov-23 7:38am    
Check the documentation to see whether it has a built in debugger, or uses an existing one from Microsoft or GNU.

OK, I have had a look at your code, and you have a number of lines like the following:
strncpy(uj->kerdes, strtok(NULL, ";"), sizeof(uj->kerdes) - 1);

So if there is a field missing in the csv your code will crash, as the call to strtok will return NULL. You need to change your code to call strtok first, and check whether it actually returns a token. Other than that, the problem could be anywhere in the remaining code.
Share this answer
420bela 17-Nov-23 7:45am    
The CSV contains no missing parts. All parts of all rows contain data with the appropriate structure.
Richard MacCutchan 17-Nov-23 7:54am    
Fine, but you still need to find out where the program crashes, and provide full details of what is happening at that point. We cannot do that for you.
420bela 17-Nov-23 8:01am    
How can I find out? I really don't mean to be obtuse, but this is the first time I've done a scan or a longer programme.
Richard MacCutchan 17-Nov-23 8:54am    
As I said, you use the debugger and step through the code. Or if that is too complicated, then you need to add more print statements to show what progress is being made. There is no simple universal rule for this, as each program is unique. It starts with finding the general area that the code is failing, and then by further iterations, narrow it down to the exact statement and the data that (possibly) causes the failure.
Richard MacCutchan 17-Nov-23 9:42am    
I have a suspicion that the problem is somewhere around the following function:
void hozzafuz(Kerdesek*eleje, Kerdesek* uj){

in file beolvas.c. At the very least it is worth checking that the pointers you are passing around are all valid.
As others have already written, this is now the third question. I have already offered some solutions. Unfortunately so far without feedback or stars.

Here are the contributions so far:

Hello! I have problems with importing CSV files in C.

Various headers are missing, but these can be ignored for the time being, as the program will crash beforehand.
// #include "bemenet.h"
// #include "dicslist.h"
// #include "idoszam.h"

Calling the "appendQuestion" function at the end of "readCSV"
void csvBeolvas(const char *fajlnev, Kerdesek **fej) 
// void readCSV(const char* filename, Questions** head)
  // appendQuestion(head, newQuestion) 
  hozzafuz(fej, uj);

should actually cause the compiler to issue a warning, as the function hozzafuz has unsuitable parameters and will not work.
Of course, the hozzafuz function should be protected against null pointers and you should also also think about what should happen if the list is still empty.
Share this answer
420bela 18-Nov-23 5:58am    
Hello! Thanks for your help, sorry I didn't appreciate your solution sooner. The page did not send a message and I did not look it up myself. Unfortunately, I have since had to modify my code and it is not working again :(
merano99 18-Nov-23 6:59am    
You will have to revise the hozzafuz() function so that it can fulfill its function. If necessary, please open a new question, copy the current source code (with translation) to the question and describe what the problem is. PS: Thanks for rating.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900