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...
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. Thanks
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
You've made a very good start actually. You just didn't know about open() and write().
And actually, there are a few arbitrary numbers for fd; when your program is run, it inherits a few from the terminal you ran it from. You could do write(1, "abcd\n", 5); to print "abcd" and a newline to the terminal for example. FD 0 is standard input(terminal keyboard if not redirected), FD 1 is standard output(terminal screen if not redirected), FD 2 is standard error(again, the terminal screen).
Standard error exists so that you can redirect standard output into a file, and still get sensible error messages printed to your terminal, or avoid printing error messages into the middle of sensible data.
You can refer to these more sensibly with STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO, defined in unistd.h
If you use a number that doesn't match any opened file, unlike stdio the program won't crash; the system calls will just return error.
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)
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)
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)
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)
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)
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)
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)
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)
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)