Scanf scansets, and reading a CSV file in C (fixed)

  Рет қаралды 10,686

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 28
@greg4367
@greg4367 Жыл бұрын
After fifty years of programming in C, I've NEVER used scanf. After this wonderful tutorial I remain convinced never to bother with it at all.
@unperrier5998
@unperrier5998 Жыл бұрын
Fifty years, wow you must be retired by now.
@aaaowski7048
@aaaowski7048 Жыл бұрын
if you dont use scanf, then you read from stdin, right? do you use the "read" function then?
@edgeeffect
@edgeeffect Жыл бұрын
The only time I've ever had to parse input data in C (that sort of shenanigans I usually do in Ruby, PHP, Python, etc) I was lucky enough that it was originally written in the age of Cobol and had fixed length fields with no delimiters. :) ... in 30ish years I might have used sscanf a couple of times... maybe.
@Bolvarsdad
@Bolvarsdad Жыл бұрын
@@aaaowski7048 The idiomatic way to take input in C is to allocate a buffer and use fread. Whether you allocate it dynamically or statically doesn't matter, if it's dynamically you can just increase the size of the buffer as your input grows, if you allocate it statically you just consume the remaining characters. You can, as you say, use the read function, nothing wrong with that. You'd simply allocate a buffer as usual, and pass a 0 to read as it represents the stdin file descriptor in unix. char buffer[200]; int input; input = read(0, buffer, 200); Be sure that you pass the same size to read as you've allocated to your buffer, else you risk buffer overflow.
@aaaowski7048
@aaaowski7048 Жыл бұрын
​@@Bolvarsdad Hate to be "that" guy Especially after being the one to ask for clarifications But i just noticed Neither read() or fread() null-terminates the resulting string. So you would need to make char buffer[200+ 1] ; To leave room for the '\0' since its stdin we're talking about. And if you dont terminate your strings manually, it may be a good idea. Only global and static arrays declared on the stack are guaranteed to be set to 0 automatically in C. There is also a "shorthand" notation to initialize local arrays allocated on the stack- whenever you initialize an array with only one element, the rest of the array is filled with 0's. So you could have void fn () { int array[100] = {1}; //inits the array to {1, 0, 0, ... 0} }
@wendolinmendoza517
@wendolinmendoza517 Жыл бұрын
What I don't like about your solution (which is very good) is that you didn't take into account the risk of reading a potentially infinite string, which is always a security issue when dealing with input. I would have liked that you address it. Very well explained, as always!
@kermitdafrog8
@kermitdafrog8 Жыл бұрын
That will probably be another video.
@edgeeffect
@edgeeffect Жыл бұрын
He did! And then he said "but I'm leaving it out for simplicity".
@zannefagerstrom5411
@zannefagerstrom5411 Жыл бұрын
I ran into a problem using scan sets or regular expressions reading a CSV file. scanf wouldn't cope with an asterix in the data
@k3v1n0x90
@k3v1n0x90 Жыл бұрын
Interesting feature of scanf It looks kind of like regex 😅
@GameBacardi
@GameBacardi Жыл бұрын
I see first time in this channel this '≠' sign. Keyboard not have that "mark" in buttons by default. But it means this !=, right ?
@ajinkyakamat7053
@ajinkyakamat7053 Жыл бұрын
Yes. Google programming font ligature.
@ramadhanafif
@ramadhanafif Жыл бұрын
Yup, I think he's using FiraCode font
@kermitdafrog8
@kermitdafrog8 Жыл бұрын
Some IDE's will change != to the new single character.
@bart2019
@bart2019 Жыл бұрын
That's a very dubious way to parse CSV files, ignoring the real difficulties with quoted values, especially when these contain newlines or a quote (represented by 2 quotes in a row).
@gerdsfargen6687
@gerdsfargen6687 Жыл бұрын
The last video was broken? Seemed ok to me?
@AMathMonkey
@AMathMonkey Жыл бұрын
The code for reading anything but a comma and a newline was previously "%[^,^ ]", but the second ^ was a bug which would have caused the ^ character to be treated the same as a comma or a newline. Someone left a comment demanding that it be fixed and the video reuploaded. I think it could have been minor enough to be addressed by a note in the description, but Jacob was nice enough to actually remake the video like the commenter requested.
@sirmokona
@sirmokona Жыл бұрын
that's C for you
@gerdsfargen6687
@gerdsfargen6687 Жыл бұрын
@@AMathMonkey thank you for clearing up.
@gerdsfargen6687
@gerdsfargen6687 Жыл бұрын
@@AMathMonkey Jacob is an exceptional teacher.
@Jacob-qx4bc
@Jacob-qx4bc Жыл бұрын
what happens when the last iteration of the code is run with a file that has values separated by a comma and a space?
@wendolinmendoza517
@wendolinmendoza517 Жыл бұрын
The space will be read as part of items
@maxaafbackname5562
@maxaafbackname5562 Жыл бұрын
Why not: ./example2 < input.csv ? The cat (process) is unnecessary here.
@31redorange08
@31redorange08 Жыл бұрын
Another reupload needed.
@philpeko1796
@philpeko1796 Жыл бұрын
@@31redorange08 Totally! (I am the culprit having claimed to reuploading. AAMOF, there are other reasons for asking a reupload...)
@edgeeffect
@edgeeffect Жыл бұрын
I've been using Unix since the 80s and I still type cat ..... pipe and then silently repeat your comment to myself..... and then do exactly the same thing next time.
@joshlengel3743
@joshlengel3743 Жыл бұрын
It's Captain, Captain Jack Sparrow
Your Variables are Not Real.
10:38
Jacob Sorber
Рет қаралды 17 М.
Scanf Basics: the good, the bad, and why so many pointers?
15:07
Jacob Sorber
Рет қаралды 24 М.
Wait… Maxim, did you just eat 8 BURGERS?!🍔😳| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 6 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 7 МЛН
How To Read a CSV File in C
13:43
HenrikM Dev
Рет қаралды 10 М.
What's the Best Way to Copy a Struct in C and C++?
13:44
Jacob Sorber
Рет қаралды 34 М.
Why Function Pointers are Awesome
11:11
Jacob Beningo
Рет қаралды 8 М.
Another way to check pointers at runtime in C
12:16
Jacob Sorber
Рет қаралды 12 М.
C Programming Reading CSV Data Files using fgets()
12:04
Kimmer Codes
Рет қаралды 14 М.
The right way to define a C function with no arguments
4:29
Jacob Sorber
Рет қаралды 34 М.
Be Careful When Using scanf() in C
12:22
NeuralNine
Рет қаралды 130 М.
One reason to Modify your Memory Allocator (C/C++)
10:23
Jacob Sorber
Рет қаралды 34 М.
Introduction to using fscanf( ) to read text
8:24
Jerry Reed
Рет қаралды 4,6 М.
Wait… Maxim, did you just eat 8 BURGERS?!🍔😳| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 6 МЛН