I am trying to open the utmp file in Linux and read the contents into an array of utmp structures. After that I display the ut_user and ut_type from each structure in the array. I have this working when I open the file with File *file and use the fopen() and fread() functions but when I try to do the same task with just a file descriptor int file and the open() and read() functions I get address locations when trying to access members of the utmp structure.
So in the below program you can see I commented out three lines of code which together I could use to successfully perform the task of reading the utmp file into an array of utmp structures and print out two of their members values. But when I try doing the exact same thing with the three lines of code (denoted "new way") in place of the old way that worked I get a bunch of address locations rather than the values of ut_user and ut_id.
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utmp.h>
void utmpprint(struct utmp *log) {
printf("\n ut_user: %s \n ut_type: %ld \n", log->ut_user, log->ut_type);
}
int main() {
int logsize = 10;
//FILE *file; //Working way
int file; //New way
struct utmp log[logsize];
int i = 0;
//file = fopen("/var/run/utmp", "rb"); //Working way
file = open("/var/run/utmp", O_RDONLY); //New way
if( file < 0 ) { //New way
printf("Failed to open");
return(0);
}
if (file) {
//fread(&log, sizeof(struct utmp), logsize, file); //Working way
read(file, &log, logsize); //New way
for(i = 0; i < logsize; i++) {
utmpprint(&log[i]);
}
close(file); //New way
} else {
return(0);
}
return(0);
}
Here is some of the output for the working way:
And here is the output for the new way that is not working:
I have tried looking online for more information on this matter but I can't seem to find anything that uses the file descriptor and not File. I also tried changing around some of the pointers and references but that did not improve any of the results.
I am very new to C and I think I am probably using the read() function incorrectly in this case because I am passing a simple buffer into the read function when I think I should be somehow passing it the utmp structure array.
You are not reading enough data from the file.
read(file, &log, logsize);
should be:
read(file, &log, sizeof(log));
// or
read(file, &log, logsize * sizeof (struct utmp));
Also, in both cases, you should check the return value to see how many bytes were actually read.
Even though you could manipulate data from the utmp file by using the open() system call or functions derived from open(), it is impractical.
You could include the "utmp.h" header in your project and use predefined functions and data structures.
Here you can find the utmp.h file documentation.
Below is some code to print the ut_user and ut_type using the utmp.h data structures and functions.
#include <utmp.h>
#include <stdio.h>
#include <string.h>
int main()
{
struct utmp* data;
char aux[50];
data = getutent();
//getutent() populates the data structure with information
while(data != NULL )
{
/*make sure to use strncpy
*because most data fiels in the utmp struct
*have ___nonstring___ atribute */
strncpy(aux, data->ut_user, 32);
aux[32] = '\0';
printf("ut_user: %s\n", aux);
printf("ut_type: %hi\n\n", data->ut_type);
data = getutent();
}
return 0;
}
The output of the code is:
ut_user: reboot
ut_type: 2
ut_user: alc
ut_type: 7
ut_user: runlevel
ut_type: 1
Related
Below is my code, I have been working on this part for some structure am create a C socket server that can process data for clients, but I got a problem in file handling. I wanted to pick data from a file and store it into a structure at specific portions and then I erase the file according to data I have picked.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "struct.h"
struct student {
char command[10];
char value[50];
char user[50];
};
int main() {
int size, i;
struct student s1[256];
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
printf("Error in reading student data");
return 1;
}
size = 0;
while(fscanf(file,"%s,%s,%s",
s1[size].command,
s1[size].value,
s1[size].user) != EOF) {
printf("%s%s%s\n",s1[size].command,s1[size].value,s1[size].user);
printf("%s\n",s1[size].command);
size++;
}
fclose(file);
return 0;
}
I wanted to pick data from my file which contains the below information and I store it in specific portions of my structure array
a,w,a
r,e,1
i.e. if I store
a in s1[0].command
w in s1[0].value
a in s1[0].user
and
r in s1[1].command
e in s1[1].value
1 in s1[1].user
But when I try running my code it prints
a, w, a in s1[0].command
and
r, e, 1 in s1[1].command
so I am kindly requesting for help because I have tried googling but I couldn't get a solution.
The "%s" format reads space-separated "words".
If there isn't a space separating the "words" you have to use some other way to parse the input. For example by reading the whole line and then use something like strtok to "tokenize" the string.
Or make sure that the input format is space-separated.
I'm working on linux, I have a file that contains a line like this:
328abc
I would like, in C, to read the integer part (328) and the characters 'a','b','c', using only the function:
ssize_t read (int filedes, void *buffer, size_t size))
This is the only thing the file contains.
I know there are better ways to do that with other functions, but I haven't coded in C for a long time, and trying to help a friend, only this function is alowed.
How do I play with the buffer to do that?
Thanks
edit:
I understand that I need to parse the buffer manually. and my question is how?
If that's the only thing in the file. This will do:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main() {
char buffer[6];
char intBuffer[4];
ssize_t bytesRead;
int number;
int fd;
if ((fd = open("file.txt", O_RDONLY)) == -1) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
if ((bytesRead = read(fd, buffer, 6)) == -1) {
perror("Error reading file");
exit(EXIT_FAILURE);
}
memcpy(intBuffer, buffer, 3);
intBuffer[3] = '\0';
number = atoi(intBuffer);
printf("The number is %d\n", number);
exit(EXIT_SUCCESS);
}
The following code will print "The number is 328".
Is this some kind of homework?
I am asking because there are better ways to do that than using the read function.
Anyway to answer your question, read reads size bytes from the file whose file descriptor is filedes and places them to the buffer.
It does not know anything about line breaks etc. So you need to manually find where a line ends, etc. If you want to only use read, then you need to manually parse the buffer after each call to read (supposing your files contains many lines, that you want to parse).
Beware that a line may be split between two read calls, so you need to handle that case with caution.
What is the easiest way to create a temporary file from C program in Linux?
We can either call system function and use mktemp
system("TMPFILENAME=$(mktemp tmp.XXXXXXXX)");
or we can use function
mkstemp function or we can use tmpfile of tmpnam functions.
Don't do this. You should be using the mkstemp function. Here's an example that prints the name of the file like you want along with other information:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
// buffer to hold the temporary file name
char nameBuff[32];
// buffer to hold data to be written/read to/from temporary file
char buffer[24];
int filedes = -1,count=0;
// memset the buffers to 0
memset(nameBuff,0,sizeof(nameBuff));
memset(buffer,0,sizeof(buffer));
// Copy the relevant information in the buffers
strncpy(nameBuff,"/tmp/myTmpFile-XXXXXX",21);
strncpy(buffer,"Hello World",11);
errno = 0;
// Create the temporary file, this function will replace the 'X's
filedes = mkstemp(nameBuff);
// Call unlink so that whenever the file is closed or the program exits
// the temporary file is deleted
unlink(nameBuff);
if(filedes<1)
{
printf("\n Creation of temp file failed with error [%s]\n",strerror(errno));
return 1;
}
else
{
printf("\n Temporary file [%s] created\n", nameBuff);
}
}
For a reference of the function look here.
This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 9 years ago.
I am supposed to create a program that takes a given file and creates a file with reversed txt. I wanted to know is there a way i can start the read() from the end of the file and copy it to the first byte in the created file if I dont know the exact size of the file?
Also i have googled this and came across many examples with fread, fopen, etc. However i cant use those for this project i can only use read, open, lseek, write, and close.
here is my code so far its not much but just for reference:
#include<stdio.h>
#include<unistd.h>
int main (int argc, char *argv[])
{
if(argc != 2)/*argc should be 2 for correct execution*/
{
printf("usage: %s filename",argv[0[]);}
}
else
{
int file1 = open(argv[1], O_RDWR);
if(file1 == -1){
printf("\nfailed to open file.");
return 1;
}
int reversefile = open(argv[2], O_RDWR | O_CREAT);
int size = lseek(argv[1], 0, SEEK_END);
char *file2[size+1];
int count=size;
int i = 0
while(read(file1, file2[count], 0) != 0)
{
file2[i]=*read(file1, file2[count], 0);
write(reversefile, file2[i], size+1);
count--;
i++;
lseek(argv[2], i, SEEK_SET);
}
I doubt that most filesystems are designed to support this operation effectively. Chances are, you'd have to read the whole file to get to the end. For the same reasons, most languages probably don't include any special feature for reading a file backwards.
Just come up with something. Try to read the whole file in memory. If it is too big, dump the beginning, reversed, into a temporary file and keep reading... In the end combine all temporary files into one. Also, you could probably do something smart with manual low-level manipulation of disk sectors, or at least with low-level programming directly against the file system. Looks like this is not what you are after, though.
Why don't you try fseek to navigate inside the file? This function is contained in stdio.h, just like fopen and fclose.
Another idea would be to implement a simple stack...
This has no error checking == really bad
get file size using stat
create a buffer with malloc
fread the file into the buffer
set a pointer to the end of the file
print each character going backwards thru the buffer.
If you get creative with google you can get several examples just like this.
IMO the assistance you are getting so far is not really even good hints.
This appears to be schoolwork, so beware of copying. Do some reading about the calls used here. stat (fstat) fread (read)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct stat st;
char *buf;
char *p;
FILE *in=fopen(argv[1],"r");
fstat(fileno(in), &st); // get file size in bytes
buf=malloc(st.st_size +2); // buffer for file
memset(buf, 0x0, st.st_size +2 );
fread(buf, st.st_size, 1, in); // fill the buffer
p=buf;
for(p+=st.st_size;p>=buf; p--) // print traversing backwards
printf("%c", *p);
fclose(in);
return 0;
}
My first post :), am starting out with C language as basic learning step into programming arena. I am using following code which reads string from text file, makes directory with that string name and opens a file for writing in that created directory. But am not able to create a file inside directory made, here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <string.h>
int main()
{
char file_name[25], cwd[100];
FILE *fp, *op;
fp = fopen("myfile.txt", "r");
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fgets(file_name, 25, fp);
_mkdir(file_name);
if (_getcwd(cwd,sizeof(cwd)) != 0)
{
fprintf(stdout, "Your dir name: %s\\%s\n", cwd,file_name);
op = fopen("cwd\\file_name\\mynewfile.txt","w");
fclose(op);
}
fclose(fp);
return 0;
}
What you need is to store the file name (with the path) in a c-string before opening. What you are opening is cwd\file_name\mynewfile.txt. I doubt that your directory is named cwd.
A sample could could be:
char file_path[150];
sprintf(file_path, "%s\\%s\\mynewfile.txt", cwd, file_name);
op = fopen(file_path,"w");
use
#include <sys/stat.h>
#include <sys/types.h>
instead of
#include <direct.h>
and modify
op = fopen("cwd\\file_name\\mynewfile.txt","w”);
I see you are using the return values. That is a good start for a beginner. You can refine your error messages by including "errno.h". Instead of printing your own error messages call
printf("%s", strerror(errno));
You get more precise error messages that way.
op = fopen("cwd\\file_name\\mynewfile.txt","w”);
You’re actually passing the string literals “cwd” and “file_name” as part of the path of the file, when I think you actually mean to put the contents of the variables with those names in there. You will probably have to piece together a string for the path. Try looking into strcat()
http://www.cplusplus.com/reference/cstring/strcat/