LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2004, 02:16 AM   #1
ej25
Member
 
Registered: Nov 2004
Posts: 39

Rep: Reputation: 15
Counting Lines


Code:
u=1;
printf("%i: ",u);
	u++;
	while ((n=read(fd1, buffer, SIZE)) != 0) {
		int i;
		
		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));
 		buffer2[1]=u
                          write(1,&buffer2[i],sizeof(char));
		u++;
		}
				
		else
		 write(1,&buffer[i],sizeof(char));

		 }

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?

Last edited by ej25; 12-03-2004 at 03:07 AM.
 
Old 12-03-2004, 02:59 AM   #2
salparadise
Senior Member
 
Registered: Nov 2002
Location: Birmingham UK
Distribution: Various
Posts: 1,736

Rep: Reputation: 146Reputation: 146
try respelling counting for a start (in the post subject line)
 
Old 12-03-2004, 03:08 AM   #3
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
Sorry, it's typing mistake
 
Old 12-03-2004, 05:39 AM   #4
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
29? NO body said something!!!!!!!!!!!
 
Old 12-03-2004, 06:03 AM   #5
okmyx
Member
 
Registered: May 2004
Location: Cornwall, UK
Distribution: Ubuntu 8.04
Posts: 464

Rep: Reputation: 31
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;
}
 
Old 12-03-2004, 06:51 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 12-03-2004, 06:57 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
why a program? Why not use `awk` thus:

awk '{print r++; $0}' <MyFileName>

End
 
Old 12-03-2004, 07:09 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by AnanthaP
why a program? Why not use `awk` thus:

awk '{print r++; $0}' <MyFileName>
Maybe because it's homework.

BTW awk is also oversized for this:
Code:
grep -n '' file.txt
There even is a standard tool to do just this:
Code:
nl file.txt
 
Old 12-03-2004, 08:00 AM   #9
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
u=1;
printf("%i: ",u);
u++;
while ((n=read(fd1, buffer, SIZE)) != 0) {
int i;

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));
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.
 
Old 12-04-2004, 01:13 AM   #10
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
I did what you said perfect_circle.
but it give me symbols rather than number,why?!!!
 
Old 12-04-2004, 04:51 AM   #11
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
post the whole program and the output

Last edited by perfect_circle; 10-07-2006 at 07:31 AM. Reason: I found a spelling mistake :)
 
Old 12-04-2004, 11:47 AM   #12
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
Code:
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
 
Old 12-04-2004, 01:31 PM   #13
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
I told u to use sprintf!!!!!
Something like:
sprintf(buffer2,"%d",u)

do
man sprintf
for more details
 
Old 12-05-2004, 02:06 AM   #14
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
display it using stdout
I do it before using putchar() and it work fine but know I want to use the stdout

Quote:
write(1,&buffer[i],sizeof(char));
So is there something wrong with what I do in the code, please tell me?

Last edited by ej25; 12-05-2004 at 02:41 AM.
 
Old 12-05-2004, 03:20 AM   #15
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
write(1,&buffer[i],sizeof(char));
I think you are using write wrong.

Try
Code:
write(1,buffer[i],sizeof(char));
You don't need the &.
buffer is a pointer, you should pass the value of buffer and not the address
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
And counting... MasterC General 18 09-24-2003 05:07 AM
counting dummyagain Programming 3 09-24-2003 02:28 AM
counting tool stand Linux - Software 3 08-23-2003 12:54 AM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration