7 Replies - 1741 Views - Last Post: 30 April 2009 - 01:41 AM Rate Topic: -----

#1 jim_chacko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 09

Finding the line number and column number of a character in a file

Posted 21 April 2009 - 04:23 AM

Hi,
i want to print the line number and column number of all the occurance of a string "//" in a given input file. I am able to count the number of occurance, but not with the column number
#include <stdio.h>
int main ()
{
  FILE * pFile;
  int c;
  int n = 0;
  pFile=fopen ("testing.c","r");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    do
    {
      c = fgetc (pFile);
      if (c == '/')
      {
		c = fgetc (pFile);
		if (c == '/')
      	{
      		n++;
		}
  	  }
    } while (c != EOF);
    fclose (pFile);
    printf ("The file contains %d comment characters (//).\n",n);
  }
  return 0;
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 21 April 2009 - 04:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding the line number and column number of a character in a file

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Finding the line number and column number of a character in a file

Posted 21 April 2009 - 05:44 AM

To work out which column a character is in you will need to bring the text into your program a line at a time (not a character at a time).

You need to get a line (hint hint).

Then you can count how many characters from the start of the line the character you are interested in appears.

Have a try at that idea and get back to us (if you want).
Was This Post Helpful? 0
  • +
  • -

#3 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Finding the line number and column number of a character in a file

Posted 21 April 2009 - 05:55 AM

If you want to do it a character at a time like that, though, you can have another counter for the column which you increment every time you fgetc() and which you print (or whatever) then reset to zero whenever you find a match.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Finding the line number and column number of a character in a file

Posted 21 April 2009 - 06:53 AM

View Postmoopet, on 21 Apr, 2009 - 04:55 AM, said:

If you want to do it a character at a time like that, though, you can have another counter for the column which you increment every time you fgetc() and which you print (or whatever) then reset to zero whenever you find a match.


Ah yes.
Good idea!
Was This Post Helpful? 0
  • +
  • -

#5 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Finding the line number and column number of a character in a file

Posted 21 April 2009 - 10:21 AM

I would use getline, then would use the find method in the string class to see where the character is on the line :)
Was This Post Helpful? 0
  • +
  • -

#6 jim_chacko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 09

Re: Finding the line number and column number of a character in a file

Posted 21 April 2009 - 09:47 PM

View Postjanotte, on 21 Apr, 2009 - 05:53 AM, said:

View Postmoopet, on 21 Apr, 2009 - 04:55 AM, said:

If you want to do it a character at a time like that, though, you can have another counter for the column which you increment every time you fgetc() and which you print (or whatever) then reset to zero whenever you find a match.


Ah yes.
Good idea!

//

I found anothere code from net which do the same . but i am not getting the output as expected. Can somebody help me with
what is wrong here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//
// 
//

int main(int argc, char** argv) {
	FILE *inFile;
	char c, buff[120], *t;
	int line = 1;
	
	if (argc != 3) {
		perror("Usage: findwordinfile [word] [fileName] n");
		exit(1);
	}
	
	inFile = fopen(argv[2], "r");
	printf("argv[1] = [%s]n", argv[1]);
	
	if(inFile == NULL){
		perror("File cannot be opened!");
		exit(1);
	}
	
	//reset the pointer to point to the begining of buff
	t = buff;
	do {
		c = fgetc(inFile);
		
		// This necessary for it to work on MS-Windows
		// In Windows, the newline goes in pair "rn"
		// Pain! Pain! Pain!
		if (c == 'r'){
			continue;
		}
		if (c == 'n'){
		   
			if (strcmp(buff, argv[1])== 0){
				printf("found [%s] at line %dn", buff, line);
			}
			line++;
			
			//Reset the buffer
			memset(buff, '', sizeof(buff));
			
			//reset the pointer to point to the begining of buff
			t = buff;
			continue;
		}
		if (c == ' '){
			
			if (strcmp(buff, argv[1])== 0){
				printf("found [%s] at line %dn", argv[1], line);
			}
			
			//Reset the buffer
			memset(buff, '', sizeof(buff));
			
			//reset the pointer to point to the begining of buff
			t = buff;
			continue;
		}
		else {
			// This is not a white space character
			// we need to collect it. Post increment the pointer 
			// to the next index of the buffer
			*(t++) = c;
		}
		//printf("buff = [%s] n", buff);   
		
	}while(c != EOF);   
	
	return (EXIT_SUCCESS);
}



** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Finding the line number and column number of a character in a file

Posted 22 April 2009 - 04:32 AM

You have been warned already.

No help unless you :code:
Was This Post Helpful? 0
  • +
  • -

#8 jim_chacko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 09

Re: Finding the line number and column number of a character in a file

Posted 30 April 2009 - 01:41 AM

I have been able to do get the line number and the column number of a special character.
Here i am trying to find "//" in the code and check whther the column number is less than 41.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(int argc, char** argv)

{

	FILE *inFile;
	FILE *outFile;
	char c1,c2;
	int len = 0;
	int i = 0,j = 0;
	int col = 0;
	int row = 1,errorRow;
	int errorFlg=0;
	char newFile[200];

	if (argc != 3)
	{
		perror("Usage: ./a.exe [word] [fileName]");
		exit(1);
	}

	len = strlen(argv[1]);

	inFile = fopen(argv[2], "r+");

	strcpy(newFile,"new\\");

	strcat(newFile,argv[2]);

	outFile = fopen(newFile, "w+");

	if(inFile == NULL)
	{
		perror("File cannot be opened!");
		exit(1);
	}


	while(c1 != EOF)
	{

		for(i=0;i<len && c1 != EOF;i++)

		{
			c1 = fgetc(inFile);

			if((c1 != argv[1][0])&&(c1 != 10) && (col < 41))

			{

			   fputc(c1,outFile);

			}

			if(c1 == argv[1][0])

			{

			   c2 = fgetc(inFile);

			   if(c2 == argv[1][1])

			   {

				   if((col == 0) || (col == 1))

				   {

					  fputc(c1,outFile);

					  fputc(c2,outFile);

					  errorRow = row;

				   }

				   else
					 if(col<41)

				   {

					  printf("<<ERROR row --> %d col --> %d\n",row,col);

					  for(j = col;j<41 && errorRow != row;j++)

					  {

						 fputc(' ',outFile);

					  }

					  errorRow = row;

					  if((c1 != 10) && (c2 != 10))

					  {

						 fputc(c1,outFile);

						 fputc(c2,outFile);

					  }

					  errorFlg == 1;

				   }

				   else if(col>41)

				   {

			
					   printf("								 >>ERROR row --> %d col --> %d\n",row,col);

					   if((c1 != ' ') && (c2 != ' '))

					   {
						  fputc(c1,outFile);

						  fputc(c2,outFile);
					   }

				   }

				   else

				   {

					  fputc(c1,outFile);

					  fputc(c2,outFile);

				   }

				   col++;

			   }

			   else

			   {

				  fputc(c1,outFile);

				  fputc(c2,outFile);

			   }

			}

			else if(c1 == 10)

			{

			  col = 0;
			  fputc(c1,outFile);
			  row ++;

			}

			else if ((col > 41) &&
					   (c1 != ' ') && (c2 != ' '))

					   {
						  fputc(c1,outFile);

						  fputc(c2,outFile);
					   }

		col++;

		}

	   };

if(errorFlg == 0)

{

	printf("\n****************NO ERRORS******************\n");

}

fclose(inFile);

fclose(outFile);

}



Thanks you all for the guidance and help

Jim

** EDIT ** :code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1