• 0

Ideas needed [C]


Question

For all of you who are familiar with linux and what the "cat" command does. I have to write a similar version of this command in C but add a few extra features. For those of you who dont know what the cat command does, It lists the contents of a file. I have to write a function that will read in and list the contents of a file but I have to add in a feature that displays the the line number before the line. Although there is a problem with this. The file is listed correctly and the line numbers are correct but for some reason it appends another line number onto the end of the program. For Example :

Contents of File :

Test1

Test2

Test3

Output from the program

1 Test1

2 Test2

3 Test3

4

Im looking for suggestions on how I could solve this, Im not looking for code. I want to be able to do this myself just ideas on how I could solve it. Apparently its not a problem thats solved easily but Im looking for members of Neowin to give me some ideas. Thanks in Advance.

Edited by Sawyer12
Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

Very easy to fix.

U either did something wrong or ur missing something that cat does. I'm guessing 1.

But without knowing how u read the file, what's the exact file ur testing on and how u display the lines, not much chance we can tell u what u did wrong or what ur missing.

Link to comment
Share on other sites

  • 0

This is apparently a common problem that is quite hard to fix. We have to make the function for university and it says at the bottom of the info sheet we got, Watch for the extra number been added onto the end. As for the first post my loop is in this format so it checks for the end of the file but I think the file pointer is on the new line at the end of the file

while(! feof(Fileptr))

{

*code in here to read in the file character by character

}

Link to comment
Share on other sites

  • 0

You can usualally fix this by checking for an "empty" line:

while(!eof)
{
    data = readline();
     if(data[0] == 0)continue;

     // code to print out, etc... //

}

You might have to modify that if you actually read in the newline.

Link to comment
Share on other sites

  • 0

Some good suggestions there,

@Andareed : We have to read the file in a character at a time so Im not sure if what you suggested would work. What do you mean by "data[0] == 0" do you mean if the data been read in is equal to NULL or is that a different scenario. Is data meant to be an array ?

@Mouton: I thought that changing the order of how I did things would help but apparently its not a simple(ish) solution like that. I print the line number out and while the character I read in is not the end of the line character i.e \n then keep reading then print out, When the newline character is reached, Increment the line number and print it out.

Edit : I was trying to look for the source code for the cat command, Im not sure if it exists or not but I couldnt find it.

Link to comment
Share on other sites

  • 0

Would this work for every file ?

while(!feof(Fileptr))

{

read in a character

if character != newline

{

increment line counter

print line counter

while (character != \n)

{

print character

read character

}

}

Im sure you all get the jist of what im trying to get across.

Link to comment
Share on other sites

  • 0

The "data[0] == 0" checks for zero-length strings and ignores them. THis would ignore final newlines, but also any other empty lines in the file.

Link to comment
Share on other sites

  • 0

Thanks for that but if the file has a blank line in I think that a line number is still printed out its only if the line at the end of the file is blank.

Here is the code I come up with, It doesnt work. it just gets stuck in an infinte loop.

#include <stdio.h>

void mycat(FILE *Fileptr);
#define NEW_LINE '/n'

int main()
{
	FILE *Fileptr;

	Fileptr = fopen("TestFile.txt","r");

	if(Fileptr == NULL)
	{
  printf("File Could not be opened");
	}
	else
	{
  mycat(Fileptr);
	}

	return 0;
}
void mycat(FILE *Fileptr)
{
	int LineCount = 0;
	char Character;

	while(!feof(Fileptr))
	{
  
  Character = fgetc(Fileptr);

  if(Character != NEW_LINE)
  {
 	 LineCount++;
 	 printf("%d",LineCount);
    
    while(Character != NEW_LINE)
    {
   	 printf("%c",Character);
   	 Character = fgetc(Fileptr);
    }
  }	
	}


	return;
}

Link to comment
Share on other sites

  • 0

#define NEW_LINE '/n'

should be

#define NEW_LINE '\n'

Why do u read char by char ? Why not read line by line ?

void mycat(FILE *Fileptr)
{
	int LineCount = 0;
	char Character, lastCharacter = NEW_LINE_LF;

	while(!feof(Fileptr))
	{
  Character = fgetc(Fileptr);
  if(!feof(Fileptr) && lastCharacter == NEW_LINE_LF)
  {
 	 printf("%d ",++LineCount);
  }
  printf("%c",Character);
  lastCharacter = Character;
	}
}

This will echo the last \n if there is one, but won't output a line number...

As I said, the key is to use the right order with the operations; only output a line number after u read a char and u know u didn't reach the eof.

Edited by Mouton
Link to comment
Share on other sites

  • 0

I have got it sorted now :) , The reason I read it character by character is because the info sheet we were given said to do so. I think later on as the program is added to there needs to be a function to search for a certain character. I got it sorted though. I now have a different problem lol :( .

Link to comment
Share on other sites

This topic is now closed to further replies.