I have a project to have a file read using argc and argv. Then sort it and do some other things. I'm having trouble with the very first step. Loading the file. This is what I have so far. Any help you be great.

#include <stdio.h>   
#include <stdbool.h>

void openFile(int argc, char *argv[]);

int main(int argc, char *argv[])
{
   openFile(argc, argv);
   printf("\n\n");
   
   return 0;
}

void openFile(int argc, char *argv[])
{
   int i;
   
   printf("\nThe number of arguments is %d", argc);   
   printf("\nThe name of the program is %s", argv[0]);
   
   for ( i = 1; i < argc; i++)
      printf("\nUser value No. %d: %s", i, argv[i]);
}

I have a project to have a file read using argc and argv. Then sort it and do some other things. I'm having trouble with the very first step. Loading the file. This is what I have so far. Any help you be great.

<snip>

Do you mean this?

#include <stdio.h>   

void openFile(int argc, char *argv[]);

int main(int argc, char *argv[])
{
   openFile(argc, argv);
   printf("\n\n");
   
   getchar();
   return 0;
}

void openFile(int argc, char *argv[])
{
   FILE *hfile;
   int i;
   
   printf("\nThe number of arguments is %d", argc);   
   printf("\nThe name of the program is %s", argv[0]);
   
   for ( i = 1; i < argc; i++)
   {
      printf("\nUser value No. %d: %s\n", i, argv[i]);
      hfile = fopen(argv[i],"r");
      if(!hfile){
            printf("Error opening file %d!",i);
      }else{
            //do what you want with the file
      }
   }
}

This is the example given to me:

$ cat t1
John Doe 4 555-55-5555 November 2,
1976
Marsupial Platypus 6 222-22-2222 May 16, 1975

Cathrine Finter 3 888-88-8888 October
28, 2000
Jane Doe
5
444-44-4444 September 27, 2000
John Walsh 5 111-11-1111 February 2, 1977

$ ./a.out 5 t1


Name Level SSN Birthdate
---- ----- --- ---------
John Doe 4 555-55-5555 11/02/1976
Marsupial Platypus 6 222-22-2222 05/16/1975
Cathrine Finter 3 888-88-8888 10/28/2000
Jane Doe 5 444-44-4444 09/27/2000
John Walsh 5 111-11-1111 02/02/1977

This leads me to believe that t1 isn't a file, but an array of some kind. Any thoughts?

%cat t1 proves t1 is a file.
What the 5 is for on the $ ./a.out 5 t1 I don't know. It's unnecessary.
The command should simply be $ ./a.out t1

So what does your program do? What did you expect it to do instead?

The program is ultimately supposed to take the command line argument then sort the list by SSN. The user is then supposed to be able to search for SSN's. I can't get passed the first stage. Right now I'm trying to get the program just to print the data from the file, so at least i know the program sees it, then I'll worry about the sorting.

The program is ultimately supposed to take the command line argument then sort the list by SSN. The user is then supposed to be able to search for SSN's. I can't get passed the first stage. Right now I'm trying to get the program just to print the data from the file, so at least i know the program sees it, then I'll worry about the sorting.

Here, now it will print the contents:

#include <stdio.h>   

void openFile(int argc, char *argv[]);

int main(int argc, char *argv[])
{
   openFile(argc, argv);
   printf("\n\n");
   
   getchar();
   return 0;
}

void openFile(int argc, char *argv[])
{
   FILE *hfile;
   int i;
   char buffer[150]; // Added this
   
   printf("\nThe number of arguments is %d", argc);   
   printf("\nThe name of the program is %s", argv[0]);
   
   for ( i = 1; i < argc; i++)
   {
      printf("\nUser value No. %d: %s\n\n", i, argv[i]);
      hfile = fopen(argv[i],"r");
      if(!hfile)
      {
            printf("Error opening file %d!",i);
      }
      else
      {
            printf("File contents:\n"); // Added this
            while(fgets(buffer,150,hfile)) // Added this
            {
                printf("%s",buffer); // Added this
            }
      }
      fclose(hfile); //I forgot to add this in the previous post
   }
}

I'll leave the sorting part to you :)

commented: Do not write the program for people. It's THEIR homework, not yours. What you've just done is called CHEATING. -3

The program is ultimately supposed to take the command line argument then sort the list by SSN. The user is then supposed to be able to search for SSN's. I can't get passed the first stage. Right now I'm trying to get the program just to print the data from the file, so at least i know the program sees it, then I'll worry about the sorting.

I asked

So what does your program do? What did you expect it to do instead?

not "what is it supposed to do eventually". You didn't answer either question...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.