I'm stuck with my program here where it reads the data from a text file but it did not obtain the float part as float.
Instead of 43.23, it prints 43.00 after it reads from my .txt file.
Where did I do wrong?
dir[k].age=atol(strtok(NULL,","));
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
atol reads a long. You probably wanted atof.
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
You are using a function that converts to integers, so that doesn't know how to handle fractional parts. Use strtof instead, that even allows for error checking, in contrast to the ato* functions.
Here:
dir[k].weight=atol(strtok(NULL,","));
dir[k].height=atol(strtok(NULL,"\n"));
You are reading values as long
Related
My goal is to parse potentially garbled ASCII data containing text and numbers. So far I've been doing fine with getting a pointer to numbers within the text with strcmp and hard-coded pointer arithmetic and then converting them with strtol, but I feel there must be a better way.
Is there a function with the effect of scanf which takes a pointer to memory instead of input stream?
You'll be wanting sscanf. Note that it does expect a null-terminated string.
Is using fscanf when opening a file in binary mode bad? I can't seem to find anything reasonable on the Internet. I am trying to open and read a PPM file and I've found this, but I am not sure if using fscanf is okay? And using netpbm is not okay, yeah.
Reading this with fread seems like a pain.
The scanf and fscanf functions are for reading characters, e.g., "1234", and converting them from a string to an integer. But integers are not stored as stings in a binary file. The actual bytes of the integer itself are stored. These need to be read directly into an integer with fread.
I have a file input, in which i have the following data.
1 1Apple 2Orange 10Kiwi
2 30Apple 4Orange 1Kiwi
and so on. I have to read this data from file and work on it but i dont know how to retrieve the data. I want to store 1(of 1 apple) as integer and then Apple as a string.
I thought of reading the whole 1Apple as a string. and then doing something with the stoi function.
Or I could read the whole thing character by character and then if the ascii value of that character lies b/w 48 to 57 then i will combine that as an integer and save the rest as string? Which one shall I do? Also how do I check what is the ASCII value of the char. (shall I convert the char to int and then compare, or is there any inbuilt function?)
How about using the fscanf() function if and only if your input pattern is not going to change. Otherwise you should probably use fgets() and perform checks if you want to separate the number from the string such as you suggested.
There is one easy right way to do this with standard C library facilities, one rather more difficult right way, and a whole lot of wrong ways. This is the easy right way:
Read an entire line into a char[] buffer using fgets.
Extract numbers from this line using strtol or strtoul.
It is very important to understand why the easier-looking alternatives (*scanf and atoi) should never be used. You might write less code initially, but once you start thinking about how to handle even slightly malformed input, you will discover that you should have used strtol.
The "rather more difficult right way" is to use lex and yacc. They are much more complicated but also much more powerful. You shouldn't need them for this problem.
Lately I had a task that included printing base-4 representation of a number. Since I didn't find a function to do it for me, I implemented it (which is not so hard of course), but I wonder, is there a way to do it using format placeholders?
I'm not asking how to implement such function, but if such function / format placeholder already exists?
There is no standard C or C++ function, but you may be able to use itoa
The closest you could get to doing it with printf is using snprintf to convert it to hex, then a lookup table to convert hex digits to pairs of base-4 digits. :-)
No, not in the Standard C library.
I think that printf can handle only decimal, hexadecimal and octal values.
So i think no.
I have a scientific application for which I want to input initial values at run time. I have an option to get them from the command line, or to get them from an input file. Either of these options are input to a generic parser that uses strtod to return a linked list of initial values for each simulation run. I either use the command-line argument or getline() to read the values.
The question is, should I be rolling my own parser, or should I be using a parser-generator or some library? What is the standard method? This is the only data I will read at run time, and everything else is set at compile time (except for output files and a few other totally simple things).
Thanks,
Joel
Also check out strtof() for floats, strtod() for doubles.
sscanf
is probably the standard way to parse them.
However, there are some problems with sscanf, especially if you are parsing user input.
And, of course,
atof
In general, I prefer to have data inputs come from a file (e.g. the initial conditions for the run, the total number of timesteps, etc), and flag inputs come from the command line (e.g. the input file name, the output file name, etc). This allows the files to be archived and used again, and allows comments to be embedded in the file to help explain the inputs.
If the input file has a regular format:
For parsing, read in a full line from the file, and use sscanf to "parse" the line into variables.
If the input file has an irregular format:
Fix the file format so that it is regular (if that is an option).
If not, then strtof and strtod are the best options.