Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,559 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,639 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

Help reading from command-line, sorting, then outputting to STDOUT, Sorting in C, using input from the command-line.

gsewell
23 Nov, 2007 - 06:59 AM
Post #1

New D.I.C Head
*

Joined: 12 Oct, 2007
Posts: 9


My Contributions
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:
CODE

#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

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Help Reading From Command-line, Sorting, Then Outputting To STDOUT
23 Nov, 2007 - 07:09 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,129



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
http://www.dreamincode.net/code/snippet488.htm
User is online!Profile CardPM
+Quote Post

gsewell
RE: Help Reading From Command-line, Sorting, Then Outputting To STDOUT
23 Nov, 2007 - 07:25 AM
Post #3

New D.I.C Head
*

Joined: 12 Oct, 2007
Posts: 9


My Contributions
QUOTE(no2pencil @ 23 Nov, 2007 - 08:09 AM) *


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

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Help Reading From Command-line, Sorting, Then Outputting To STDOUT
23 Nov, 2007 - 07:52 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,129



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
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.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month