Hi check my program i have used to read a FILE( nice.ls is shown below the program). I am able to read the FILE nice.ls, but i also have to check whether it starts with interger and next i have to check the hexadecimal if no hexadecimal then it should check for character.

I am able to read the hexadecimal but not able to read if hexadecimal is not present, PLZ help me..

# include <stdio.h>
#include<conio.h>
#define TRUE 1

 int main()
{
   FILE * pFile;
   char stg [1024];
   int i=0;
   char line[1024];
   char *ch;
   char *p;
   unsigned long int addr,opcode;
	int j,k,l,m;
	char n;
   char inst1[10],inst2[10];

   pFile = fopen ("nice.ls" , "r");

   if (pFile == NULL) perror ("Error opening file");
   else {
	   while(ch =fgets (stg , 1024 , pFile)) {
		   if(ch =='\n') {
			   line[i] = '\0';
			   return i;
		   }
		   else line[i] = ch;
		   i++;
	      
		
		   if( fscanf(pFile, "%d",&j)== TRUE){
			   //printf("%d\n",j);
			 if(fscanf(pFile,"%s",&inst1) == TRUE ) {
				   printf(" %d  %s \n",j,inst1);
			 }}//else if (fscanf(pFile,"%
		/*	  else if(fscanf(pFile,"%x",&k) == TRUE) {
				  if(fscanf(pFile,"%x",&l) == TRUE)
				  printf("%d %x %x\n",j, k,l);
				//   if(fscanf(pFile,"%x",&l)==TRUE ) {
				//	   if(fscanf(pFile,"%s",&inst1) == TRUE) {
				//		   if(fscanf(pFile,"%s",&inst2) == TRUE) 
			//  printf("%d %x %x    %s  %s \n",j,k,l,inst1,inst2);
			  }} */
		   else printf("error");
		//  fscanf( pFile, "%x",&k);
		//   fscanf( pFile, "%x", &l);
	
		//   fscanf( pFile, "%s %s",&inst1,&inst2);

		//	   printf("%d   %x    %x      %s  %s\n",j,k,l,inst1,inst2);
		  // sscanf(ch,"%d %lx %lx %s %s ",&j,&addr,&opcode, inst1,inst2);
		  // printf("%d  %lx      %lx      %s %s\n",j,addr,opcode,inst1,inst2);
	   }
   }
   fclose (pFile);
   getch();
 }

MY nice.ls FILE

1                     ; C Compiler for M68HC08 (COSMIC Software)
   2                     ; Generator V4.5.10.1 - 14 Jun 2007
   3                     ; Optimizer V4.5.4.2 - 17 Jul 2007
   5                     	xref	_printf
  32                     ; 1 void main(void)
  32                     ; 2 {
  33                     	switch	.text
  34  0000               _main:
  38                     ; 3 printf(" hello world "); 
  40  0000 a600          	lda	#low(L71)
  41  0002 ae00          	ldx	#high(L71)
  43                     ; 4 }
  46  0004 cc0000        	jmp	_printf
  58                     	xdef	_main
  59                     .const:	section	.text
  60  0000               L71:
  61  0000 2068656c6c6f  	dc.b	" hello world ",0
  62                     	end

Your program is really hard to read, you should use comments and explain what each part does or your going to have a lot of trouble debugging larger programs.

I'm not sure as I haven't tested your code but I don't think your
program really does anything other than check whether your file exists. Because your first else statement all you do is copy the first line to stg and then you ignore stg and go onto checking ch is NULL and make the first element of line to be NULL as well.

I think you should scrap this and start fresh and take it one step at a time.

1. Check file exists
2. If exists copy all the data using fscanf or fgets to your variables
3. Once you reach end of file leave the loop.
4. Test your variables to see whether they match a Hex or string and then produce your output.

line 22: fgets() doesn't return '\n'. Read this . If the line read contains '\n' then the last character in the input buffer will also be '\n' and in that case you need to strip it off if you want the line to be useful.

if( stg[strlen(stg)-1] == '\n')
    stg[strlen(stg)-1] = 0;

line 24: the value of variable i is uninitialized so all it contains is some random value.

And don't post a program with half the lines commented out. Remove them first to clean things up a little.

Your program is really hard to read, you should use comments and explain what each part does or your going to have a lot of trouble debugging larger programs.

I'm not sure as I haven't tested your code but I don't think your
program really does anything other than check whether your file exists. Because your first else statement all you do is copy the first line to stg and then you ignore stg and go onto checking ch is NULL and make the first element of line to be NULL as well.

I think you should scrap this and start fresh and take it one step at a time.

1. Check file exists
2. If exists copy all the data using fscanf or fgets to your variables
3. Once you reach end of file leave the loop.
4. Test your variables to see whether they match a Hex or string and then produce your output.

Thanks kenji, now i realize my mistakes
PLZ check my modified program,
i am able to read the nice.ls FILE.
Let me explain u my nice.ls FILE content
1st column is integers dont bother abt them,
2nd column is address, i read it by using isxdigit function and i am able to read the hexadecimal
3rd column is opcode , i am also able to read them
4th column are instructions, i am not able to read

i am able able to understand why it is not reading the 4th column
PLZ give me some suggestions

# include <stdio.h>
#include<conio.h>
#include<ctype.h>

 int main()
{
   FILE * pFile;
   char mystring [1024];
   char line[1024];
   unsigned long int address,opcode;
   char inst1[1024],inst2[10];

   pFile = fopen ("null.ls" , "r");

   if (pFile == NULL) perror ("Error opening file");
   else 
	   while(fgets (mystring , 1024 , pFile) != NULL)
	   {
		   if(isxdigit(mystring[6]))
		   {
			   address = strtol(mystring+6,NULL,16);
			   opcode = strtol(mystring+11,NULL,16);
			 //  printf("%lx %lx \n",address,opcode);
			   sscanf(mystring+26,"%s",&inst1);
			   printf("lx %lx %s \n",address,opcode,inst1);
		   }
	   }
	   fclose (pFile);
	   getch();
 }

And don't post a program with half the lines commented out. Remove them first to clean things up a little.

my apologize for not commenting
PLZ check my program, i have managed to read(nice.ls FILE) the hexadecimal from 6th and 11th column, but failed to read 26th column instructions.

# include <stdio.h>
#include<conio.h>
#include<ctype.h>

 int main()
{
   FILE * pFile;
   char mystring [1024];
   char line[1024];
   unsigned long int address,opcode;
   char inst1[1024],inst2[10];

   pFile = fopen ("nice.ls" , "r");

   if (pFile == NULL) perror ("Error opening file");
   else 
	   while(fgets (mystring , 1024 , pFile) != NULL)   //reads the file nice.ls line wise and stores in mystring
	   {
		   if(isxdigit(mystring[6]))     //this will check for hexadecimal in the 6 column of nice.ls file
		   {
			   address = strtol(mystring+6,NULL,16);     //it will store 6th column hexadecimal(address)
			   opcode = strtol(mystring+11,NULL,16);     //it will store 11th column hexadecimal(opcode)
			 //  printf("%lx %lx \n",address,opcode);
			   sscanf(mystring+26,"%s",&inst1);         //it will store the instruction, but i failed to use correctly
			   printf("lx %lx %s \n",address,opcode,inst1);  // i am able to check 6th and 11th column(hexadecimal or not) but in the 26th column i have to read the instructions(not able to read these instructions)
		   }
	   }
	   fclose (pFile);
	   getch();
 }
This article has been dead for over six months. Start a new discussion instead.