954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading Files With argc argv

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]);
}
arineon
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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.

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
      }
   }
}
darkbreaker
Newbie Poster
21 posts since Nov 2010
Reputation Points: 43
Solved Threads: 3
 

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?

arineon
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

%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?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

arineon
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 
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 :)

darkbreaker
Newbie Poster
21 posts since Nov 2010
Reputation Points: 43
Solved Threads: 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 askedSo 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...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: