p.s. there was a topic with same files i guess my class mates
Main objective :
1.Shows data (in table format) from a binary file
2.If file does not exist show nothing
3.Then ask for user input to fill data
4.Saves user input to file
5.Exits
1. so this is the main code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
struct student {
char name[20];
char lastname[20];
unsigned int course;
char ReasonToKill[20];
};
void data_input(struct student *data, int num)
{
int idx = 0;
while (num-- > 0) {
printf("Enter Name,Lastnamem, course(numbers only),"
"ReasonToKill(press enter after each section Andrejs[Enter]"
" Ivanovs[Enter] "
"2[Enter] Uglymot..[Enter]\n "
"if more then one student this message will repeat: ");
scanf("%s %s %d %s", (data + idx)->name, (data + idx)->lastname,
&(data + idx)->course, (data + idx)->ReasonToKill);
idx++;
}
}
void data_show(struct student *data, int num)
{
int idx = 0;
printf("Name |"
" LastName |" " Course |" " ReasonToKill \n");
while (num-- > 0) {
printf("%-20s| %-20s| %-7d| %-10s\n", (data + idx)->name,
(data + idx)->lastname,
(data + idx)->course, (data + idx)->ReasonToKill);
idx++;
}
}
int main(int argc, char **argv)
{
int num;
struct student *sp = NULL;
umber of students to kill:");
scanf(" % d ", &num);
sp = (struct student *)malloc(sizeof(struct student) * num);
if (!sp) {
printf(" Memory allocation failed \ n ");
return -1;
}
data_input(sp, num);
data_show(sp, num);
free(sp);
return 0;
}
this is the code to create(if not created),save to(overwrites) bin file but it has function to save only data that is already given from the start
2.
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
struct student {
int group;
int kurss;
};
int main(int argc, char **argv)
{
int fd;
int bytes = 0;
char *file = "data.bin";
struct student data = {
.group = 3,
.kurss = 4
};
fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0000777);
if (fd == -1) {
printf("File open error: %i (%s)\n", errno, strerror(errno));
return fd;
}
bytes = write(fd, &data, sizeof(data));
if (bytes == -1) {
printf("File write error: %i (%s)\n", errno, strerror(errno));
close(fd);
return bytes;
}
printf("Student data saved to %s (%i bytes)\n", file, bytes);
close(fd);
return 0;
}
this the 3rd code
it functions as the 2nd
only it can not create a bin file if no file detected it will give a error in command promp(logicaly), it can save file(adding more values with out overwriting the the old ones)
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
struct student {
int group;
int kurss;
};
int main(int argc, char **argv)
{
int fd;
int err = 0;
char *file = "data.bin";
struct student data = {
.group = 2,
.kurss = 1
};
fd = open(file, O_APPEND | O_RDWR, 0000777);
if (fd == -1) {
printf("File open error: %i (%s)\n", errno, strerror(errno));
return fd;
}
err = write(fd, &data, sizeof(struct student));
if (err == -1) {
printf("File write error: %i\n", err);
close(fd);
return err;
}
printf("%lu bytes saved to %s\n", sizeof(struct student), file);
close(fd);
return 0;
}
Question 1 how to make 2nd or 3th to:
create if not created, save(adding more values like 3rd code) without [FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]overwriting the old file.
Question 2 how to implement a program(a 2nd+3rd code to the 1st one) so it would read all values:
that I have inputed and save it(like code 3 and create if not created a bin file with these values).
As the 1st program is base of structure that users inputs while 2nd and 3rd is from values that are set in the code.
really need help with these ones and would very much appreciate
the comments in the code.
4. code it reads the bin file (not that important)
it must be seperate.
Question 3 (if possible) how to make it read the the 1st code.
Because its size of that of 2. and 3.
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
struct student {
int group;
int kurss;
};
int main(int argc, char **argv)
{
int fd;
int err = 0;
int bytes = 1;
int count = 0;
int idx = 0;
char *file = "data.bin";
struct student *data = NULL;
struct student *tmp;
fd = open(file, O_RDWR, 0000777);
if (fd == -1) {
printf("File open error: %i (%s)\n", errno, strerror(errno));
return fd;
}
data = (struct student *) malloc(sizeof(struct student));
while (bytes > 0) {
bytes = read(fd, data + count, sizeof(struct student));
if (bytes == -1) {
printf("File read error: %i (%s)\n", errno, strerror(errno));
close(fd);
free(data);
return errno;
}
if (!bytes)
break;
if (bytes < sizeof(struct student)) {
printf("Error: read %i bytes out of %lu\n",
bytes, sizeof(struct student));
close(fd);
free(data);
return errno;
}
count++;
tmp = (struct student *) realloc(data,
sizeof(struct student) * (count + 1));
if (!tmp) {
close(fd);
free(data);
return errno;
}
data = tmp;
}
printf("group\tkurss\n");
while (count--) {
printf("%i\t%i\n", (data + idx)->group, (data + idx)->kurss);
idx++;
}
printf("Read %i records from %s\n", idx, file);
close(fd);
free(data);
return 0;
}