1
  1. In this program, i want to let user to input 2 arguments, the number of integer,and the file name.

    1. the file has 10 lines of integer value.
    2. read the file, and put it to inArray[];
    3. and then output it as the end;

      notes: For the complete program, i want to make a program that will scan a file consists of random integer,and then sort them in ascend order, and print out the first 10 percent of the sorted integer.

      Error: For now, i want to test if it can read the file and put values into the inArray properly, but its keep getting errors.

        warning: initialization makes integer from pointer without a cast
         findTotal.c:43:6: warning: passing argument 1 of ‘fopen’
                     makes pointer from integer without a cast
        /usr/include/stdio.h:271:14: note: expected ‘const 
         char * __restrict__’ but argument is of type ‘char’
      

Please help me with this, thank you

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(int argc, char *argv[]){

 int numOfInt;
 char fileName="";

 sscanf(argv[1],"%d",&numOfInt);
 sscanf(argv[2],"%c",&fileName);

 int i, rc;

 /* the origninal list , initialise as 0 index*/
 int inArray[numOfInt];

 /* the number of output int  */
 int outNumInt = numOfInt * 0.1;

 /*  the output array of int  */
 int outArray[outNumInt];


 FILE *inFile;
 inFile = fopen(fileName,"r");

 /*      check if the file is empty      */
 if(inFile==NULL){
    printf("can not open the file");
 }

 for (i = 0; (rc = getc(inFile)) != EOF && i < numOfInt; inArray[i++] = rc) 
 { 


 }//for

 fclose(inFile);

 for(i = 0; i < numOfInt;i++){

    printf("%x\n",inArray[i]);
 }



}//main
2
  • 1
    What's the problem that you're having? What errors are you getting? Commented Nov 4, 2012 at 17:40
  • May I suggest using fscanf to read in the numbers in the file? Commented Nov 4, 2012 at 17:43

3 Answers 3

1

I think you could be using scanf better here. You use it to read in two pieces of information that should have been passed as arguments to the program, and then refreain from using it for what it would actually be good for, which is reading the file in question. Here is my take at this:

#include <stdlib.h>
#include <stdio.h>
int cmp(const void *a, const void *b) { return *(int*)b - *(int*)a; }

int main(int argc, char *argv[])
{
    char * ifile = argv[1];
    int n = atoi(argv[2]), m = n/10, i;
    int nums[n];
    FILE * f = fopen(ifile, "r");
    for(i = 0; i < n; i++) fscanf(f, "%d", &nums[i]);
    qsort(nums, n, sizeof(int), cmp);
    for(i = 0; i < m; i++) printf("%d\n",nums[i]);
    return 0;
}

If this file is prog.c and the corresponding executable is prog, and your file with numbers is called nums.txt, and contains 100 integers, you would call this as

prog nums.txt 100

The advantage of taking in parameters this way is that it makes repeating the command later easier (all the information needed to repeat it will be in the command history of the shell), and that it is the standard way of passing parameters to a program. It also frees up standard input for other uses.

Sign up to request clarification or add additional context in comments.

Comments

0

You have indeed a problem with the filename's management. char is for characters; if you want to handle a file name, you have to use a string. In C, we can use an array of char, terminated by a nul-character. Here, because argv[2] holds directly the name, you can simply use a pointer.

char *fileName = argv[2];

And then:

fopen(fileName, "r");

Since you don't modify the argv pointer, you can also send directly argv[2] as argument.

Comments

0

One of the problems I see in your code is:

 char fileName="";
 sscanf(argv[2],"%c",&fileName)

A string literal is a constant string, which means you shouldn't attempt to modify it, you should either use a static (or dynamic) char array for that string and use the %s format specifier, or just point fileName to argv[2]

char *fileName;    
fileName = argv[2];

4 Comments

char fileName="" does not make any sense. That stores the address of the constant string "" into a char (which is too small to store the address). The sscanf won't try to modify the string literal but only change the value stored in fileName.
@Maxime it doesn't do what you say exactly but anyway that's not my code, that's the OP's code :) I'm just quoting the problem
I know, I know. If you think I've said something wrong, feel free to correct me ;).
@Maxime actually never mind, I didn't read what you said carefully, sorry :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.