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 Жыл бұрын
Fifty years, wow you must be retired by now.
@aaaowski7048 Жыл бұрын
if you dont use scanf, then you read from stdin, right? do you use the "read" function then?
@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 Жыл бұрын
@@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 Жыл бұрын
@@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 Жыл бұрын
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 Жыл бұрын
That will probably be another video.
@edgeeffect Жыл бұрын
He did! And then he said "but I'm leaving it out for simplicity".
@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
@iNTERnazionaleNotizia589Ай бұрын
Hi I have question, is it possible for the 'While loop (scanf)' to be implemented in OpenMP? I want to read from a CSV file, 25000 rows, and I want to parallelize that with openmp Please give me advice on how to do that (or source)
@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 Жыл бұрын
Yes. Google programming font ligature.
@ramadhanafif Жыл бұрын
Yup, I think he's using FiraCode font
@kermitdafrog8 Жыл бұрын
Some IDE's will change != to the new single character.
@k3v1n0x90 Жыл бұрын
Interesting feature of scanf It looks kind of like regex 😅
@gerdsfargen6687 Жыл бұрын
The last video was broken? Seemed ok to me?
@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 Жыл бұрын
that's C for you
@gerdsfargen6687 Жыл бұрын
@@AMathMonkey thank you for clearing up.
@gerdsfargen6687 Жыл бұрын
@@AMathMonkey Jacob is an exceptional teacher.
@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 Жыл бұрын
The space will be read as part of items
@maxaafbackname5562 Жыл бұрын
Why not: ./example2 < input.csv ? The cat (process) is unnecessary here.
@31redorange08 Жыл бұрын
Another reupload needed.
@philpeko1796 Жыл бұрын
@@31redorange08 Totally! (I am the culprit having claimed to reuploading. AAMOF, there are other reasons for asking a reupload...)
@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.
@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).