|
Home > Archive > Unix Programming > February 2006 > need help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| hi group
i am trying to write a c program to get a file name from the
user and read that file and copy all the contents into a new file using
only unbuffered i/o in the following manner
input file
Once upon a time
In a far away land
There lived a ...
output file
There lived a ...
In a far away land
Once upon a time
Please help me i am getting frustrated with this
Ganesh
| |
| Fred Kleinschmidt 2006-02-17, 10:40 pm |
|
"gman" <ganeesh@gmail.com> wrote in message
news:1140018145.864528.253840@g44g2000cwa.googlegroups.com...
> hi group
> i am trying to write a c program to get a file name from the
> user and read that file and copy all the contents into a new file using
> only unbuffered i/o in the following manner
> input file
> Once upon a time
>
> In a far away land
> There lived a ...
>
> output file
> There lived a ...
> In a far away land
>
> Once upon a time
>
> Please help me i am getting frustrated with this
> Ganesh
>
What have you tried so far? Why does it have to be unbuffered, other than
that is what the homework assignment says to do?
Start by accepting the file names (from stdin, or as command line
arguments).
Open the two files, one in read mode, the other in write mode. Be sure to
check for success.
If the input file is reasonably short, just read each line into an array of
strings, then write the strings to the output file in reverse order.
Then close the files.
If the file is very long, you might need to try something else (it might not
fit entirely in memory).
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
| |
|
| well what i did is this
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
off_t nread,seeke,j;
int fin,fout,i;
i=0;
char buf[512];
char *buf2;
fin=open(".g",O_RDWR);
fout=open("f",O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR |
S_IXUSR);
seeke=lseek(fin,0,SEEK_END);
j=seeke;
lseek(fin,0,SEEK_SET);
nread=read(fin,buf,seeke);
// printf("outside for");
while(i<strlen(buf))
{
printf("inside while");
if(buf[i]!="\n")
{
buf2[i]=buf[i];
}
else
{
j=j-seeke;
lseek(fout,j,SEEK_SET);
write(fout,buf2,strlen(buf2));
}
i++;
}
return 0;
}
everything works fine but nothin is getting copied and yes this is my
homework and i am new to c in unix... previously i used to do turbo c
so i am having lots of confusion...
Ganesh
| |
| Bit Twister 2006-02-17, 10:40 pm |
| On 15 Feb 2006 19:30:45 -0800, gman wrote:
<snip>
> everything works fine but nothin is getting copied and yes this is my
> homework and i am new to c in unix...
then I'll suggest adding a -ansi and -g switch, and stepping through
it with a debugger.
| |
|
| i will try that right a way thanks for the tip... i had a tough time
thinking how to debug
Ganesh
|
|
|
|
|