PDA

View Full Version : write function of system call failing



learnunix
10-16-2014, 12:11 PM
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.

-----------------------


#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.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[I], sizeof(struct record));
}

close(fp);
exit(0);
}

Salem
10-16-2014, 12:44 PM
> for(i=4 ; i<=0 ; i--)
When do you expect this to be true?

Perhaps you meant >=

Also, you're going to write the same record every time.

learnunix
10-16-2014, 03:43 PM
NIce. You where right Salem. BUT the output is not on the .txt file I create. It is on the terminal an I get a strange output (unresolved characters). Some character would not work on the forum here so I've copy/paste it on a .txt file an added it as image 13725attachement (it's a screenshot ). Below is the new 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[i], sizeof(struct record));
}

close(fp);
exit(0);
}

Salem
10-16-2014, 10:25 PM
> BUT the output is not on the .txt file I create. It is on the terminal an I get a strange output
That's because you're writing binary data.

If you want to see text, then you need to format your data as a string (say sprintf), then write the whole string to the file.

But in doing so, you lose the ability to easily seek to specific records.

Try this command


od -Ax -t x1z mydatafile

Nominal Animal
10-17-2014, 12:15 AM
if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
The above line is equivalent to


fp = open(argv[1], O_WRONLY | O_CREAT, 0644) < 0;
if (fp)
You can fix your line by adding parentheses, but I personally prefer to do the assignment first, and then check the result -- I find it easier to read.

Because fp ends up becoming logical false, value 0, and that happens to be what STDIN_FILENO macro evaluates to, and you are running this in a terminal, you get the output into the terminal instead of to the file. (Normally, standard input is not writable, but in case of Unix/POSIX terminals and pseudoterminals, it usually is writable too; just a copy of standard output and/or standard error file descriptors.)

learnunix
10-17-2014, 08:20 AM
Writring binary data...that makes sense.. I mean at least I should get 1s and 0s in my .txt file.
Still I'm having issues
1st : I tried the code you gave me....The result is 000000 in my terminal. And still nothing in my .txt file
2nd : In the code below, you'll see I tried using sprintf. The result is I get what I want in the terminal (all character resolved) and still nothing in my .txt file


#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];
int i,fp;
char mystr[8];

if(fp = open(argv[1], O_WRONLY | O_CREAT ,0644) <0)
{
perror(argv[1]);
exit(1);
}

strcpy(user[1].login,logins[1]);
sprintf(mystr,"%s", user[1].login);
write (fp, mystr, 8);
}

close(fp);
exit(0);
}