So my assignment is make a dynamic array to read integers from a file, which the first integer is the size used to create a dynamic array. But I am lost at how to specifically take the first integer.

main(int argc, char **argv) {
  float average;                  /* SET this variable to computed average */
  int small, second_small;      /* and these hold the smallest values */
  FILE *input_file;
  char filename[32];
  if ((argc == 2)&&(strcmp(argv[1],"-d")==0)) input_file = open_file();
  else {
     if (argc != 2) {printf("Usage: %s <filename>\n",argv[0]); exit(2);}
     if ((input_file = fopen(argv[1],"r")) == NULL) {
        printf("Unknown file: %s\n",argv[1]); exit(3);
     }
  }

I dont ask for code, I asking on how to handle the file. Thank you guys.

Recommended Answers

All 11 Replies

Why are you using command line parameters (argc/argv) for this?

Are you guaranteed that the file has the correct data so you don't need much error checking? If so, you can use fscanf() to read the values.

If not, use fgets() to read a line and check if the correct characters are there before converting to integers.

The code is from the professor, so i assume she is right :P

Member Avatar for I_m_rude

well, I can't judge your proffessor. But as I said, you can use freopen() if you want. As WaltP said, you can use fscanf() and fgets() if you want as per the correctness of data he explained. http://pubs.opengroup.org/onlinepubs/009604499/functions/freopen.html this is another link which i think will be helpful to you. thanks if i am helpful to you. :-)

The code is from the professor, so i assume she is right :P

In my classes, I never assume the professor gave good data. I assume they will throw in bad data to see if we can handle it. It's your call.

you can use freopen() to read or write data from/into the file

Why freopen() specifically? freopen() is actually a fairly rare function to need, usually being the choice for unusual stuff like redirecting standard streams from code or changing the open mode of an existing stream.

But I am lost at how to specifically take the first integer.

This isn't a complex problem. You need to add all of the necessary code after the file is opened:

main(int argc, char **argv) {
  float average;                  /* SET this variable to computed average */
  int small, second_small;      /* and these hold the smallest values */
  FILE *input_file;
  char filename[32];
  if ((argc == 2)&&(strcmp(argv[1],"-d")==0)) input_file = open_file();
  else {
     if (argc != 2) {printf("Usage: %s <filename>\n",argv[0]); exit(2);}
     if ((input_file = fopen(argv[1],"r")) == NULL) {
        printf("Unknown file: %s\n",argv[1]); exit(3);
     }

     /* Your code here */

  }

Which would probably consist of an else clause with declarations for the size of the array and the pointer for allocating memory. Then you can simply use fscanf() to read the size, and allocate the array:

main(int argc, char **argv) {
      float average;                  /* SET this variable to computed average */
      int small, second_small;      /* and these hold the smallest values */
      FILE *input_file;
      char filename[32];
      if ((argc == 2)&&(strcmp(argv[1],"-d")==0)) input_file = open_file();
      else {
         if (argc != 2) {printf("Usage: %s <filename>\n",argv[0]); exit(2);}
         if ((input_file = fopen(argv[1],"r")) == NULL) {
            printf("Unknown file: %s\n",argv[1]); exit(3);
         }
         else {
            int size;

            if (fscanf(input_file, "%d", &size) != 1)
                puts("Invalid format");
            else {
                int *array = malloc(size * sizeof *array);

                if (!array)
                    puts("Insufficient memory");
                else {

                    /*
                        Read the file's subsequent contents into array
                    */

                }
            }
         }
      }

No tricky stuff, just adding basic code to the partial program you've been given.

As i am just a beginner, Can i throw some light or can i give my answer on the questions which are posted on the daniweb. I don't know whether i am allowed to do so or not. I have seen many threads on daniweb of which i know the answers. So admins can i give my answers there(for ex, here) ?

As i am just a beginner, Can i throw some light or can i give my answer on the questions which are posted on the daniweb.

Of course you're allowed to answer questions. That's one of the best ways to learn. However, please refrain from providing complete solutions to homework questions when the OP hasn't shown any real effort at solving it on their own.

but if i get wrong in my answers or if there is any mistake in my answers, then ? Will you ban me from this site ?

but if i get wrong in my answers or if there is any mistake in my answers, then ?

Then you'll probably be corrected by someone who recognizes the mistake and knows how to fix it. That's how answering questions can help you learn.

Will you ban me from this site ?

You have a very strange idea of how this forum works. We don't ban people unless they break our rules (and even then only after doing so multiple times), and our rules are common sense things like "don't break the law", "don't spam the forum", and "don't harrass other members".

If we banned anyone who made a mistake in an answer then there wouldn't be any members left. ;)

hahahah.. that line was damn ridiculous that If we banned anyone who made a mistake in an answer then there wouldn't be any members left. ;)

it's okay. thank you for this info. i was thinking something else. i will never ever break any rule and will live here forever like a good guy and will share my doubts with daniweb.com. ;)

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.