Help reading from command-line, sorting, then outputting to STDOUT

Sorting in C, using input from the command-line.

Page 1 of 1

3 Replies - 1133 Views - Last Post: 23 November 2007 - 08:52 AM Rate Topic: -----

#1 gsewell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-October 07

Help reading from command-line, sorting, then outputting to STDOUT

Post icon  Posted 23 November 2007 - 07:59 AM

My aim is to write a program that will accept a file from the command-line, sort that file's lines of text into alphabetical order, and output to STDOUT.

The closest thing I have is a way to sort arguments themselves, like so:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main (int argc, char *argv[])
{
	int i, comp;
	for(i = 0; i < argc; i++)
	{
		if((comp = strcmp(argv[i],"c"))==0)
	{
			printf("Here is an argument: %s",argv[i]);
		
		else if (comp < 0)
			printf("%s is less than 'c'", argv[i]);
		else if (comp > 0)
			printf("%s is greater than 'c'", argv[i]); 			
	}
	return 0;
}



The text file that will be fed in as an argument can be sorted by it's first letter, or whatever is easiest, the part that matters is reading in the file and printing to STDOUT.

Thanks a lot guys and gals of <DIC/>
-G

Is This A Good Question/Topic? 0
  • +

Replies To: Help reading from command-line, sorting, then outputting to STDOUT

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Help reading from command-line, sorting, then outputting to STDOUT

Posted 23 November 2007 - 08:09 AM

http://www.dreaminco.../snippet488.htm
Was This Post Helpful? 0
  • +
  • -

#3 gsewell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-October 07

Re: Help reading from command-line, sorting, then outputting to STDOUT

Posted 23 November 2007 - 08:25 AM

View Postno2pencil, on 23 Nov, 2007 - 08:09 AM, said:



I spent an hour checking the snippets before posting. I needed an example a little closer to what I am trying to do.

Thanks,
Gary
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Help reading from command-line, sorting, then outputting to STDOUT

Posted 23 November 2007 - 08:52 AM

I did not see any code in your project to actually open & close the file. That snippet should give you a head start.

Once you are opening the file, then you can start reading in the data.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1