ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying in this code to read a file and display it using stdout with numbering each line in the file, but there is somthing wrong with the numbering. I don't what's it. Any help, please?
I'm no expert and its a long time since i've done any coding.
To my knowledge most programming languages have the ability to read individual lines from a file so this would remove the need to do all that \n checking
pseudo code:
linenum = 1;
while not fromfile.EOF {
stdout.write( linenum + ":" + readline(fromfile) );
linenum=linenum+1;
}
It would be significantly easier if you use the stdio functions instead of the low-level read(). That way it would be quite straight-forward to implement okmyx' pseudo code.
write(1,&buffer[i],sizeof(char)); buffer2[1]=u; <- use sprintf if you want to output the number u contains into buffer2 (if buffer2 is a char array)
write(1,&buffer2[i],strlen(buffer2)*sizeof(char)+1);
u++;
}
else
write(1,&buffer[i],sizeof(char));
}
Last edited by perfect_circle; 12-03-2004 at 08:04 AM.
int fd1;
char buffer2[SIZE];
char buffer[SIZE];
fd1 = open(argv[c2], O_RDONLY);
u=1;
if (fd1 == -1) {
fprintf(stderr, "Cannot open file %s\n", argv[c2]);
return 2;
}
/* display the file */
printf("\nThe contents of the %s file:\n",argv[c2]);
printf("%i: ",u);
u++;
while ((n=read(fd1, buffer, SIZE)) != 0) {
int i;
buffer2[1]=u;
for (i=0; i<n ; i++){
if(buffer[i] == '\n' && buffer[i+1]== 0 )
write(1,&buffer[i],sizeof(char));
else if(buffer[i] == '\n' ){
write(1,&buffer[i],sizeof(char));
write(1,&buffer2[1],strlen(buffer2)*sizeof(char)+1);
u++;
}
else
write(1,&buffer[i],sizeof(char));
}
this the output:
Code:
The contents of the p2.c file:
#include <stdio.h>
#include<string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
main (int argc, char *argv[] ){
int i;
int status;
int c=1;
int forkresult ;
char array[20];
char line[80];
char *num, *count,*begin;
fgets(line, 80, stdin);
forkresult = fork ( ) ;
if (forkresult != 0)
/* the parent will execute this code */
wait(&status);
else /* forkresult == 0 */
7
7
7
7 /* the child will execute this code */
7
7
7
7 num=line;
7 begin=line;
7 count=array;
7 line[strlen(line)]==' ';
7 for(num;*num<strlen(line);num++){
7 if(line[(int)num]==' '){
7 array[(int)count]=(int)malloc((num-begin)+1);
7 strncpy(&array[(int)count],&line[(int)begin],(int)begin-strlen(line));
7 begin=num+1;
7 count++;
7 }
7}
7 array[(int)count]=NULL;
7
7
7 /*execvp (argv[1],&argv[1] );
7
7*/
7
7 execvp (array, NULL);
R
R
R
R printf ("EXEC Failed\n") ;
R
R /* This above line will be printed only on error and not otherwise */
R
R
R exit (1) ;
R
R
R}
R
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.