So the following program doesn't know how many files will receive that's why I used a vector of pointers to store the pointers to all files. Anyway, the user must input from stdin a vector of strings and the reading stop when /exit
is entered, but before that the command ./program file1 file2 ... fileN
must be executed as well. After fileN
the program takes the next following strings as command line arguments, which is not what I intended.
First:
./program file1.txt file2.jpg file3.in
and stdin:
sort
alg
di
pi
food
/exit
Have a look:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{ char c;
//Allocate memory for the file array based on the number of command line arguments
FILE **f = malloc((argc-1)*sizeof(FILE*)); //invalid conversion from ‘void*’ to ‘FILE**’ {aka //‘_IO_FILE**’} [-fpermissive]
int i;
for (i=1; i<argc; i ) {
printf("%s\n", argv[i]);
f[i - 1] = fopen(argv[i],"r");
}
while((c= getchar()) != '\n' && c != EOF); //invalid conversion from ‘void*’ to ‘char**’ //[-fpermissive]
char **text=malloc(300*sizeof(char*));
for (i=0; i<=30; i ) //invalid conversion from ‘void*’ to ‘char**’ [-fpermissive]
text[i]=malloc(300*sizeof(char));
char s[30];
int N=0;
int ok=0;
while (ok==0)
{
fgets(s,30,stdin);
strcpy(text[N], s);
if (strncmp(s,"/exit",5)==0)
ok=1;
N ;
}
for (i=0; i<N; i )
printf("%s\n", text[i]);
// free memory after usage
free(f);
for(int i=0;i<30;i )
free(text[i]);
free(text);
return 0;
}
When I press the run button it gives some weird errors like :
invalid conversion from ‘void*’ to ‘char**’ [-fpermissive]
or
invalid conversion from ‘void*’ to ‘FILE**’ {aka ‘_IO_FILE**’} [-fpermissive]
(see code).
I changed the malloc to char text[300][300]
but I still could not read the text from stdin.
It should print the vector of strings but it doesn't do anything if I pass command line arguments and if I press run it just gives the errors above.
The program is meant to simply read file names as command line arguments (minimum number of arguments is 1 and maximum 9) and then a vector of strings from stdin and print the vector of strings in stdout.
(The files passed as command line arguments will be needed later to search matching strings with the strings from the vector of strings. I did not write code for that since the vector of strings is not read properly)
Can you help me read the command line arguments and the vector of strings at the same time?
CodePudding user response:
The error message invalid conversion from ‘void*’ to ‘char**’
will appear
if you are compiling the source code as c
. Cast the result as
text = (char **)malloc( .. )
to avoid it.
If you are compiling the code as c
, such error will not occur.
Based on your requirements, would you please try the following:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EXIT_CODE "/exit"
int main(int argc, char *argv[])
{
char **text = NULL; // array of strings
char line[BUFSIZ]; // line buffer while reading from stdin
char *p; // temporal pointer on the input line
int n = 0; // line counter of stdin
int i; // general index
FILE **fp; // array of file pointers
if (NULL == (fp = malloc((argc - 1) * sizeof(FILE *)))) {
perror("malloc");
return EXIT_FAILURE;
}
for (i = 1; i < argc; i ) { // open files in the arguments
printf("%s\n", argv[i]);
if (NULL == (fp[i - 1] = fopen(argv[i], "rb"))) {
perror(argv[i]);
return EXIT_FAILURE;
}
}
while (fgets(line, BUFSIZ, stdin)) {
if ((p = strrchr(line, '\n'))) *p = '\0'; // remove trailing newline, if any
if ((p = strrchr(line, '\r'))) *p = '\0'; // remove trailing cr character, if any
if (NULL == (text = realloc(text, (n 1) * sizeof(char **)))) {
perror("realloc");
return EXIT_FAILURE;
}
if (NULL == (text[n] = malloc(strlen(line) 1))) {
// allocate memory for the input line
perror("malloc");
return EXIT_FAILURE;
}
strcpy(text[n], line);
n ; // increment the line counter
if (strncmp(line, EXIT_CODE, strlen(EXIT_CODE)) == 0) break;
}
// show the input from stdin
for (i = 0; i < n; i )
printf("%s\n", text[i]);
/*
* do your processings of the input files here
*/
// free memory after usage
for (i = 1; i < argc; i )
fclose(fp[i - 1]);
free(fp);
for (i = 0; i < n; i )
free(text[i]);
free(text);
return EXIT_SUCCESS;
}