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

  Рет қаралды 11,040

Jacob Sorber

Jacob Sorber

Күн бұрын

Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.th...
Website ➤ www.jacobsorbe...
---
Scanf scansets, and reading a CSV file in C // Let's talk a bit more about scanf, specifically how to get more control in how scanf reads in strings. In fact, let's read in a CSV file and do something with it.
Related Videos:
Scanf video: • Scanf Basics: the good...
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorbe...
people.cs.clem...
persist.cs.clem...
To Support the Channel:
like, subscribe, spread the word
contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtub...]

Пікірлер: 29
@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.
@unperrier
@unperrier Жыл бұрын
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".
@iNTERnazionaleNotizia589
@iNTERnazionaleNotizia589 2 ай бұрын
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)
@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
@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.
@k3v1n0x90
@k3v1n0x90 Жыл бұрын
Interesting feature of scanf It looks kind of like regex 😅
@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
@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).
@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 М.
What is an object pool, and how to create one in C?
23:14
Jacob Sorber
Рет қаралды 15 М.
ВЛОГ ДИАНА В ТУРЦИИ
1:31:22
Lady Diana VLOG
Рет қаралды 1,2 МЛН
"Идеальное" преступление
0:39
Кик Брейнс
Рет қаралды 1,4 МЛН
Scanf Basics: the good, the bad, and why so many pointers?
15:07
Jacob Sorber
Рет қаралды 25 М.
How To Read a CSV File in C
13:43
HenrikM Dev
Рет қаралды 11 М.
When do I use a union in C or C++, instead of a struct?
11:18
Jacob Sorber
Рет қаралды 71 М.
I made Tetris in C, this is what I learned
15:15
Austin Larsen
Рет қаралды 27 М.
C Programming Reading CSV Data Files using fgets()
12:04
Kimmer Codes
Рет қаралды 14 М.
Be Careful When Using scanf() in C
12:22
NeuralNine
Рет қаралды 131 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 23 М.
What's the Best Way to Copy a Struct in C and C++?
13:44
Jacob Sorber
Рет қаралды 35 М.
Read And Write An Array Of Structs To A Binary File | C Programming Example
18:27
How to Send and Receive UDP packets (in C)
23:20
Jacob Sorber
Рет қаралды 8 М.
ВЛОГ ДИАНА В ТУРЦИИ
1:31:22
Lady Diana VLOG
Рет қаралды 1,2 МЛН