Hello Guys,
My problem here is that I have a C code that is suppose to write data to a file (preferably a .txt file). But after I run the code it creates the file but it does not write anything inside. PLEASE HELP..I'm a newbie (and I don't want to give up now please) The code is below.
-----------------------
Code:
#include <sys/file.h>
#include <stdio.h>
struct record{
int uid;
char login[8];
};
char * logins[] = { "user1", "user2", "user3", "user4", "user5" };
int main (int argc, char * argv[])
{
//struct record * user[5]; At first I tried to use an array of pointer but Hmmm had enough trouble to try with a simple structure. lol
struct record user[5];
int i,fp;
if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
{
perror(argv[1]);
exit(1);
}
for(i=0 ; i<=4 ; i++)
{
user[i].uid = i;
strcpy(user[i].login , logins[i]);
}
for(i=4 ; i<=0 ; i--)
{
lseek(fp, (long) i*sizeof(struct record), L_SET);
write (fp, &user, sizeof(struct record));
}
close(fp);
exit(0);
}