Extracting information from lines of a file in C [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I would like to extract file names and their corresponding MD5 sum from a check sum file in a format such as this-
MD5 (FreeBSD-8.2-RELEASE-amd64-bootonly.iso) = 2587cb3d466ed19a7dc77624540b0f72
I would prefer to do this locally within the program, which rules out awk and the like.

You can read lines easily enough using fgets(). Don't even think of using gets().
If you're reasonably confident you won't be dealing with filenames containing spaces or close parentheses, you can use sscanf() to extract the bits and pieces:
char hash_type[16];
char file_name[1024];
char hash_value[128];
if (sscanf(line, "%15s (%1023s) = %127s", hash_type, file_name, hash_value) == 3)
...good to go...
else
...something went wrong...
Note the sizes specified in the sscanf() string compared to the variable definitions; there isn't an easy way to generalize that other than by using snprintf() to create the format string:
char format[32];
snprintf(format, sizeof(format), "%%1%zus )%%%zus) = %%%zus",
sizeof(hash_type)-1, sizeof(file_name)-1, sizeof(hash_value)-1);
Your alternative is some routine forward parsing to locate the hash type and the open parenthesis before the start of the file name, and some trickier backwards parsing, skipping over the hash value and finding the equals and the last close parenthesis, and then collecting the various parts.

You should be able to implement this with fopen(), fgets() and strchr() - but first you will need to nail down the format of the file more precisely (for example: what happens if the filename includes a ) character?)

I wouldn't advocate it in most languages, but why not just hit it up with POSIX regex?

Related

Representation of a C binary file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
For a homework assignment I created a simple compression/decompression program that makes use of a naive implementation of run-length encoding. I've gotten my program working; compressing and decompressing any text file with a pretty large number of characters (e.g. the program source) works flawlessly. As an experiment I tried to compress/decompress the binary of the compression program itself. This resulted in a file that was much smaller than the original binary, and is obviously un-runnable. What is causing this data-loss?
My assumption was that it's related to how binary files are represented, but I can't figure much out past that.
Possible issues:
Your program opens the binary file in the text mode, which damages the '\r' and '\n' bytes
Your program incorrectly handles zero bytes, treating them as ends of strings ('\0') and not as data of its own
Your program uses char (that is actually signed char) for the bytes of data and correctly works only with non-negative values, which ASCII chars of English text are, but fails to work with arbitrary char/byte values, which may be negative
Your program has an overflow somewhere which shows up only on big files
Your program has some other data-dependent bug
If the platform is linux (as the question is tagged), there's no difference between binary and text modes. So it shouldn't be that; but even so, the files should be opened as binary.
I suspect that your problem is the program treats '\0' characters as terminators (or otherwise specially) instead of as valid data.

What is the meaning of f in printf? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What is the meaning of "f" in C's printf?
The f in printf stands for formatted, its used for printing with formatted output.
As others have noted, the trailing f indicates formatted output (or formatted input for functions in the scanf family).
However, I'll add that the distinction matters because it's important for callers to know that the string is expected to have format-specifier semantics. For example, do not do this:
char* s = get_some_user_input();
printf(s); // WRONG. Instead use: printf("%s", s) or fputs(stdout, s)
If s happens to contain % characters, printing it directly with printf can cause it to access non-existent arguments, leading to undefined behavior (and this is a cause for some security vulnerabilities). Keep this naming convention in mind if you ever define your own printf-like variadic functions.
If I'm not mistaken, printf stands for "Print formatted data to stdout".
printf allows for formatting, while print doesnt. Also, print doesn't exist in C. I don't even know what printg is.

Opening a file into a struct [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I seem to be having an issue with opening a file containing information e.g student records and printing it using my struct.
Say I have student names, their IDs and grades in the txt file such as:
casnova 195843 A
and so on...
I defined my struct as:
struct student {
char name[20];
int ID;
char Grade;
};
I want to load up this file using structs, and I am kind of lost as to what I should be doing.
My question is not asking how to do the question but basically a starting point as I am still fairly new to C programming, any tips/links are helpful :)
My question is not asking how to do the question but basically a
starting point
Define an array of struct student an array of char
Use fopen to open the file
Use fgets in a while loop to read one line at a time (that's what the char array is for)
Use sscanf to extract constituent members of said line. Test the value returned by sscanf. Alternatively you can use strtok and convert tokens as needed.
The old answer is off topic and is erased.
Didn't noticed that was a text file.
You may want to find the answer of two things, or you may already know the answer:
read text file with fscanf
write date to a struct variable
If what's in the file is something like:
Tom 123 A
Dick 456 B
and you open the file with fopen, and get a FILE * called fp.
struct student s;
fscanf(fp, "%s %d %c", s.name, &s.ID, &s.Grade);
will fill the struct with the content in the file.
But you must make sure the content of the file is correct. Or you will have to do some complex parsing with the content of the file.

Reverse the place of the string in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want reverse the string in particular format.
For example, "My name is Nishant" should be converted to "Nishant is name My".
If you have your words in a char[] words array then it is a simple loop:
for (i = 0; i < mid; i++)
exchange(words[i], words[number_of_words - i]);
for sane definitions of mid, number_of_words and exchange.
If all you have is a big char containing the entire statement, doing a strtok first is helpful. Then, use the above loop.
Give our regards to your instructor. If this is a homework assignment, you should write the code yourself.
Here is a little hint, though: use a char pointer to iterate through each character in the array until you hit the NUL terminator at the end. Now iterate in reverse until you hit a space. Save your place in another pointer, move forward by one then copy each of the characters up to but not including the NUL into your output buffer.
Now retrieve the position of that last space in that other pointer where you saved your place, and back up again. When you move forward, you actually need to stop when you encounter either a space of a NULL - ASCII '\0' or a zero byte - and not just a NUL.
It would be a little faster if you save the positions of each of the spaces in some kind of list as you iterate forward at the beginning. That way you don't then need to iterate backward over the entire string, with short iterations over each word. The code would be a little more complicated.
The increased efficiency would be insignificant for short strings like individual English sentences, but would be quite a lot of you were reversing a large file that you just read into memory.

When should I use fputs instead of fprintf? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
What exactly is the difference between the two?
fprintf does formatted output. That is, it reads and interprets a
format string that you supply and writes to the output stream the
results.
fputs simply writes the string you supply it to the indicated output
stream.
fputs() doesn't have to parse the input string to figure out that all you want to do is print a string.fprintf() allows you to format at the time of outputting.
As have been pointed out by other commenters (and as it's obvious from the docs) the great difference is that printf allows formatting of arguments.
Perhaps you are asking if the functions are equivalent where no additional arguments are passed to printf()? Well, they are not.
char * str;
FILE * stream;
...
fputs(str,stream); // this is NOT the same as the following line
fprintf(stream,str); // this is probably wrong
The second is probably wrong, because the string argument to fprintf() is a still a formating string: if it has a '%' character it will be interpreted as a formatting specifier.
The functionally equivalent (but less direct/efficient/nice) form would be
fprintf(stream,"%s", str);
Uhm...
...puts() just writes a string, while printf() has a number of formatting facilities for several types of data.
fputs()
http://www.cplusplus.com/reference/clibrary/cstdio/fputs/
fprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/
Documentation is useful! Learn to read it, and you'll have a powerful tool on your side.

Resources