[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: How to detect a "blank" line.



Hi, 

The code in JAVA is as following:
************************************************

import java.lang.* ;
import java.io.* ;

public class PolicyServer { //: main class 

	public static void main (String [] args ) {
	
		byte buf[]= new byte[64];
		
		int i;
		int linecounter=0;
		String s[]= new String[20];
	
		String s1=new String();
		
		try {
			DataInputStream in = new DataInputStream(new
BufferedInputStream(new FileInputStream("test.txt")));
			
			while((s1=in.readLine()) !=null){ // EOF
			
				if (s1.charAt(0)!='#') { // not a comment
or NULL line
				
					s[linecounter]=s1;
					linecounter++;
					
				}//end of if
			
			} // end while
			
				
		}//try;
		
		catch (Exception e) {
		
			System.out.println("Error: "+ e.toString());
		}////end try-catch;
		
		
		for (i=0; i<linecounter; i++)
		System.out.println("s[" + i +"]="+s[i]);
	
	} // main();



}//: end of PolicyServer

************************************************************




The code in C is as following:
/***************************************/

void trim_comment(char *buf, int len)
{
	int i;
	for ( i=0; i<len+1 && buf[i]!='\0'; i++ )
	{
		if ( buf[i]=='#' )
		{
			buf[i]='\0';
			return;
		}
	}
	buf[i]='\0';
}


#define MAX_line_len 80



int main (int argc, char **argv)
{

	/* testing functions */

	
	struct LSA  lsa;	
	
	FILE *in_file;   /*input file */
	char line[MAX_line_len];
	const char *FILE_NAME="lsa.txt"; /* read/init from lsa.txt */
	int i; /*index  */
	
	char router_id[20][20], links[2], link_id[20][20], hosts[2],
host_id[20][20];
	
	
	if (argc!=2 || (argv[1][0]!='-' || argv[1][1]!='r'))
	{
		printf("Command is not correct! \n");
		printf("Usage: LS -r --- to show you the LS
information\n");
		return -1;
	}
	
	/*lsa=malloc(sizeof(struct LSA));*/
	
	
	/* initialization -- read the lsa.txt in */
	
	
	/* IO with file; to read ls_r.txt and display for user ***/
	
	in_file=fopen(FILE_NAME, "r"); /*open the lsa_r.txt for read only
*/
	
	if (in_file==NULL)
	{
		printf("Can't open %s\n", FILE_NAME);
		return -2;
	}		
	
	i=1;
	
	
	
	/* ok, now we can read the file and show it for users */
	 
	 while (fgets(line, MAX_line_len, in_file) != NULL)
	 {
		
		/* trim comments */
		trim_comment(line, MAX_line_len);
		
		/*if( line[0]=='#' )
		{ 
			printf("comments\n");
			continue;
		}*/
		 /** comments **/
		
		
		/* check whether it is a blank line or garbage line */
		if (strlen(line) <=1) /*null line, garbage, etc */
		{
			continue;
		}
		printf("i=%d\n",i);
		
		if (i==1)  /* router_"id */
		{
			sscanf(line, "%s", router_id);
			
			(lsa.ls_id)= (u_int32) router_id;
			
			printf("DEBUG\n");
			printf("The router_id =%s\n", (lsa.ls_id));
			i++;  /* next one will be link_numbers */
			continue;
		}
		
		
		if (i==2) /* link_numbers */
		{
			sscanf(line, "%s", links);
			
			
			(lsa.ls_links)=(u_int16) atoi(links);
			
			
			printf("The link_number =%d\n", (lsa.ls_links));
			i++;  /* next one will be link_ids */
			continue;
		}
		
		/* for link_id,  2<i< (lsa->ls_links + 2 +1) */
		
		if ( i>2 && i<=(lsa.ls_links+2)) /* link_id */
		{
			
			sscanf(line, "%s",link_id[i-3]);
			lsa.lnk_id[i-3]=(u_int32) link_id[i-3];
			printf("The link_id[%d] is %s\n", i-3,
lsa.lnk_id[i-3]);
			
			lsa.BW[i-3]=get_link_BW(i-3); /* DEBUG */
			i++;
			continue;
		}	

		/* next will be host_numbers */
		if (i == (2+1+lsa.ls_links)) /* host_numbers */
		{
			
			sscanf(line, "%s", hosts);
			
			lsa.ls_hosts=(u_int16) atoi(hosts);
			printf("The host_numbers =%d\n", lsa.ls_hosts);
			i++;  /* next one will be host_id */
			continue;
		}
		
		/* The  (2+1+1+lsa->ls_links) <= host_number
<(2+1+1+lsa->ls_links+ lsa->ls_hosts) */
		
		
		 if ( i>(3+lsa.ls_links) && i<=(lsa.ls_links+
lsa.ls_hosts+3)) /* link_id */
		{
			
			sscanf(line, "%s",host_id[i-4-lsa.ls_links]);
			
			lsa.lnk_host_id[i-4-lsa.ls_links]=(u_int32)
host_id[i-4-lsa.ls_links];
			
			printf("The link_host_id[%d] is %s\n",
i-4-lsa.ls_links, lsa.lnk_host_id[i-4-lsa.ls_links]);
			
			/*for link_host, we don't need the info of BW */
			i++;
			continue;
		}	
		
		
		
	} /*end of while loop */
	
	
	printf("The route_id =%s \n", (lsa.ls_id));
	printf("The link_number(router)=%d\n", lsa.ls_links);
	printf("The link_number(host)=%d\n", lsa.ls_hosts);
	
	for (i=0; i<lsa.ls_links; i++)
	{
		printf("link_router[%d]=%s\n", i, lsa.lnk_id[i]);
	}
			
	for (i=0; i<lsa.ls_hosts; i++)
	{
		printf("link_host[%d]=%s\n", i, lsa.lnk_host_id[i]);
	}
	
	
	/*  show the LSA for me */
	
	LSA_show_info(&lsa);
	
	
	printf(" function test is successful !!!!\n");
	return 0;	

}

/************************************************************/



C.L.

On Wed, 7 Jul 1999, Matthias Pfisterer wrote:

> Please show us the code you used. That would make it much easier to
> understand your problem and to help you.
> 
> Matthias Pfisterer
> 
> 
> Chien-Lung Wu wrote:
> > 
> > Hi,
> > I try to read in a text file like follow,
> > 
> > # comments
> > #
> > line1 with information
> > line2 (blank)
> > line3 with information
> > line4 (blank)
> > # comments
> > #comments
> > 
> > I write my program in C  and also try to use JAVA. One thing very
> > interesting is my program can read-in this text file line by line (ether
> > in C or in JAVA) if no any blank line. If I edit the text file with some
> > blank line, I ether can not trin this line out (in C) or I get a error
> > message " line out of range". How can I detect a blank line and trim it
> > out?
> > 
> > Any suggestion will be appreciated. Thanks,
> > 
> > C.L.
> > 
> > ----------------------------------------------------------------------
> > To UNSUBSCRIBE, email to java-linux-request@xxxxxxxxxxxxxxxxxx
> > with a subject of "unsubscribe". Trouble? Contact listadm@xxxxxxxxxxxxxxxxxx
> 

****************************************************************************
Chien-Lung Wu						cwu4@xxxxxxxxxxxx
Graduate Student of ECE 				(O) 919-513-1894
at North Carolina State University			(H) 919-233-6724
****************************************************************************



- --------------------------------------------------------------------
For more information on Linux in India visit http://www.linux-india.org/
Please do not post HTML email to this mailing list.  HTML mails will be
thoroughly ignored and derisively sniggered at in private.

------------------------------