Sponsored Content
Full Discussion: read() contents from a file
Top Forums Programming read() contents from a file Post 302599540 by cylus99 on Friday 17th of February 2012 11:46:58 AM
Old 02-17-2012
read() contents from a file

Hi, I'm trying to implement a C program on ubuntu which reads the contents of a file that is passed in as an argument and then displays it to the screen. So far I've cobbled together this from bits online but most of it is probably wrong as its all copied and pasted...
Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main (argc, char *argv[])

{
char buf[20];
size_t nbytes;
ssize_t bytes_read;
int fd;

nbytes = sizeof(buf);
bytes_read = read(fd, buf, nbytes);

dump_buffer();

return 0;
}

void dump_buffer(void *buffer, int buffer_size)
{
int i;

for(i = 0;i < buffer_size;++i)
printf("%c", ((char *)buffer)[i]);
}

The main bit i don't get is what the 'fd' argument is thats passed into the read() function? surely doesn't that need to be some reference to the file not just a random int?? I'm also not supposed to be using high level functions life fopen() etc. Could someone please point me where to go and list the (probably many) errors i've made. ThanksSmilie

Last edited by joeyg; 02-17-2012 at 01:48 PM.. Reason: Please wrap scripts and data with CodeTags; makes easier to identify and read
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read File and use contents to rename another

Hello guys, thank God that I found this forum. I hope that someone can help me because I don't have any idea on how to start it. I know that for some of you this is a very simple task but I'm not as advance on shell scripting like many people out there. I got this file with a permanent... (10 Replies)
Discussion started by: Shark Tek
10 Replies

2. Shell Programming and Scripting

how to read the contents of a file using PERL

Hi My requirement is to read the contents of a fixed length file and validate the same. But am not able to read the contents of the file and when i tried it to print i get <blank> as an output... I used the below satatements for printing the contents ... (3 Replies)
Discussion started by: meva
3 Replies

3. Shell Programming and Scripting

Read contents of the file and print

AT ---------- 0 Elapsed: 00:00:00.02 SO ---------- 0 Elapsed: 00:00:00.01 SE ---------- 0 Elapsed: 00:00:00.01 CR ---------- (4 Replies)
Discussion started by: sandy1028
4 Replies

4. Shell Programming and Scripting

Read contents from a file

Hi Friends, I am new to this forum. Just struck up with a logic. I have a csv file seperated by ":" (colons). This csv file contains hostname and groups as follows: HOSTNAME:VT Group SGSGCT2AVPX001:Team1 SGSGCT2AVPX003:Team2 SGSGCT2AVPX005:Team2 PHMNCTTAVPX001:Team3 I want to... (2 Replies)
Discussion started by: dbashyam
2 Replies

5. Shell Programming and Scripting

script to read the contents of a file and print

Hi, Need help in writing a script to read the contents of this file test Test 00a 00b 00c 00d 00e 00f where it need to read each line to give a display such as form meta from dev 00a , config=Striped; add dev 00b:00f to meta 00a Can any one help me in writing this script (2 Replies)
Discussion started by: praveen1516
2 Replies

6. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

7. Homework & Coursework Questions

How to read contents of a file into variable :(

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to read the contents of each field of a file creating user accounts. The file will be of format : ... (6 Replies)
Discussion started by: dude_me5
6 Replies

8. Shell Programming and Scripting

how to read contents of file?

I have made a script something like this. I want it to read the contents of either file or directory but 'cat' and 'ls' is not working. Can anyone help me? I am a newbie in scripting so dont know much about it. I also dont know how can i put my code separatly on this forum #!/bin/bash echo... (9 Replies)
Discussion started by: nishrestha
9 Replies

9. Shell Programming and Scripting

read contents of a file using AWK

Hi, I am kind of new at awk programming, so any help would be great ! I am trying to read a date from a file into a variable and a count into another variable and display both these variables. The file looks like the attached file... I tried this but it doesn't work ... ... (6 Replies)
Discussion started by: RDR
6 Replies

10. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies
setproplist(3)						     Library Functions Manual						    setproplist(3)

NAME
setproplist, fsetproplist - Assigns Extended File Attributes to a file SYNOPSIS
#include <sys/proplist.h> int setproplist ( char *path, int follow, int nbytes, char *buf ); int fsetproplist ( int fd, int nbytes, char *buf ); PARAMETERS
Points to a file that is to be assigned Extended File Attributes. If non-zero, specifies that if the last component in *path is a symbolic link, then the link should be traversed. Specifies the size of *buf in bytes. Points to a buffer of Extended File Attributes. Specifies a file descriptor for a file that is to be assigned Extended File Attributes. This parameter is used with the fsetpro- plist function. DESCRIPTION
The setproplist function assigns a buffer of Extended File Attributes, pointed to by nbytes, to a file pointed to by *path. The Extended File Attribute buffer is defined in sys/proplist.h. An Extended File Attribute is a name and value pair that is contained in a variable- sized structure called a Property List. A Property List is part of a file's metadata and can contain abstract name and value pairs (Extended File Attributes) that can be set either by the operating system (for example, ACLs and privileges) or by a user-level application (for example, PC File Attributes). One or several Extended File Attributes can be assigned to a file each time setproplist is called. Also, both new and existing Extended File Attributes can be assigned to a file, although if you specify an Extended File Attribute name that already exists in the Property List and a new value to pair with it, the new value will replace the existing value. Note that any space allocated to a UFS file's Property List is accounted for in the user's quota statistics and must be accounted for in the st_blocks field returned by stat(2). The fsetproplist function behaves the same as setproplist, except that it operates on a file descriptor rather than a pointer to a file. NOTES
Although not a requirement, you should use the sizeof_proplist_entry(3) and add_proplist_entry(3) functions to initialize the Extended File Attribute buffer passed to setproplist(3) and fsetproplist(3). RESTRICTIONS
If a system failure occurs when the Extended File Attributes are being written to disk, one of the three following conditions may apply to each Extended File Attribute, since there are no ordering guarantees when several Extended File Attributes are being updated: The new value for an existing Extended File Attribute was written to disk. The new value for an existing Extended File Attribute was not written to disk and the old value still exists. A new value for a Extended File Attribute that did not previously exist was not written to disk. EXAMPLES
#include <sys/proplist.h> main() { char *ptr, *buf, *name, *value; int *value_len, *flags, buffer_size, min_buffer_size, ret, nbytes; static char *names[] = { "primary_name", "secondary_name", "" }; /* * How big a buffer do I need to store my name and value * pair in a property list ? */ buffer_size = sizeof_proplist_entry("primary_name", 18); buffer_size += sizeof_proplist_entry("secondary_name", 13); /* * Malloc the buffer */ buf = ptr = (char *)malloc(buffer_size); /* * Call add_proplist_entry to initialize the buffer with * the first name and value pair. */ ret = add_proplist_entry("primary_name", 0, 18, "Primary Name Value", &ptr); /* * Call add_proplist_entry to initialize the buffer with * the second name and value pair. */ ret += add_proplist_entry("secondary_name", 0, 13, "Another Value", &ptr); if (ret != buffer_size) { printf("ret %d != buffer_size %d0, ret, buffer_size); free(buf, buffer_size); exit(1); } /* * Buffer now contains both name and value pairs. Call setproplist * system call to actually associate name and value pairs to * file. */ nbytes = setproplist("/tmp/foo", 1, buffer_size, buf); if (nbytes < 0 || nbytes != buffer_size) { perror("setproplist"); free(buf); exit(1); } . . . RETURN VALUES
If successful, a value greater than or equal to zero is returned which represents the number of bytes of the Extended File Attributes that were assigned to the file pointed to by *path and written to disk. In addition, the change time of the file pointed to by *path is marked for update. If unsuccessful, the integer -1 is returned and errno is set to indicate the error. ERRORS
In addition to errors associated with open(2), the function will fail if: Search permission was denied for a directory in *path. The calling program was not the owner of the file and the process does not have the appropriate system privilige. A problem was encountered with the Extended File Attribute buffer. A problem was encountered with the Extended File Attribute buffer. There was an error reading or writing some portion of the Property List. The Extended File Attribute could not be associated with the file pointed to by *path. The calling program does not have the appropirate system privilige to access the requested Extended File Attribute, for example, DEC_AUDIT_PROPLISTFLAG. The file system is mounted read-only. RELATED INFORMATION
Functions: open(2), stat(2), add_proplist_entry(3), delproplist(3), fdelproplist(3), fgetproplist(3), fsetproplist(3), get_pro- plist_entry(3), getproplist(3), setproplist(3), sizeof_proplist_entry(3). Files: proplist(4), sys/proplist.h. delim off setproplist(3)
All times are GMT -4. The time now is 11:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy