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.
Related
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.
Could someone help me in printing the following data in C, exact format specifier and procedure to print.
union
{
char c[8];
short s[4];
long l[2];
void * vfp[2];
} info;
When i try using printf("%s\n", info.c); and printf("%s\n",info.s). I got some garbage values.
Also i need help in printing those pointers.
Thanks for the support.
You need to understand that the usage semantics of a union. You can only read the type that you stored inside a particular object instance of union. And you can only store one type in a union at a particular instance in time.
So either you stored s or c, it cannot be both. If your intention is to store both the types then what you need is a structure and not a union.
First of all you're using a union. the memory is shared between those 4 arrays. I'm not sure you want that.
Second, the list of format specifiers is here. %s is for strings. %p is for pointers, and %ld can be used for a signed long.
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 would like to read from a file into an unsigned character buffer as well as write that information back into a file. I am using an unsigned character buffer because I need to send this information over a UDP socket.
The problem is I can't seem to find a way to properly read the file from the buffer and write from the buffer.
Can anyone point a way to do this?
Thanks so much
Take a look at write and read functions, or fread and fwrite. They should do the trick.
For example, you write a buffer to a file with:
int fd = open("file", O_CREAT | O_WRONLY, 0600);
write(fd, yourBuffer, numberOfCharactersToWrite);
The write function may return some error codes, so read it's manual.
fwrite is very similar in usage, look at the site here.
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.
we been asked to write a c program that copy all content from dir to another even if it have a nested directories.... that is ok with me to copy files from directory to another but the problem is how can I know that I'm pointing now on the other directory and I should access it and bring files inside it ...?
I used mainly opendir, closedir and read dir
As a first approach, check if your environment supports the d_type field in the dirent structure. If that fails, you need to call stat() for each file.
You can use stat() to determine the type of the file system object. Notice that stat() accepts the path to the file so you need to construct the path of the file system object being queried as opposed to just using the d_name member of the struct dirent* returned by readdir().
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.
For example, I have a file, which says
char,5
int,6
Reading the above file, is it possible to declare 2 variable array in the code? So in future
if i add a new line it will automatically declare?
No, not in C.
You will need to write a script which reads this file and writes the c program.
In short, what you need is a C Source Code Generator.
Sure, just code exactly what you want. You can start with a structure that can hold either a character or an integer (with some boolean or integer to indicate which). Then you can allocate an array of them of any size.
When you read the first line, create an array of 5 such structures. Set their type variable to "char".
When you read the second lien, increase the size by 6. Set those six new ones to be integers.
And so on.
You can use an enum to track the type of each entry in the array. You can use a struct to hold the integer value, character value, (or just re-use the integer value) and type. You can make helper functions like isInteger, setIntegerValue, getCharacterValue, and so on.
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?