Z
Zach
My program opens a file then associates a stream with it using fdopen.
Then it reads from the stream into the buffer. I am getting a few
warnings. I compiled with very strict options:
gcc -g -W -Wall -std=c99 -pedantic
So I wanted to know if these warnings are something to worry about
(need fixing) or if I can just ignore them:
main5.c: In function ‘main’:
main5.c:30: warning: implicit declaration of function ‘fdopen’
main5.c:30: warning: assignment makes pointer from integer without a
cast
main5.c: At top level:
main5.c:11: warning: unused parameter ‘argc’
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAXLINE 90
int main(int argc, char *argv[])
{
char buf[MAXLINE];
int *psz;
int file = 0;
FILE *fpin = {0};
struct stat fileStat;
if((file = open(argv[1], O_RDONLY)) == -1)
{
printf("Cannot open file.\n");
perror("Unable to open sample.dat");
return EXIT_FAILURE;
exit(1);
}
printf("Leaving first if loop...\n");
if((fpin = fdopen(file, "r")) == NULL)
{
printf("Cannot get file descriptor.\n");
perror("fdopen failed");
close(file);
return EXIT_FAILURE;
exit(1);
}
printf("About to call fstat()...\n");
if ((fstat(file,&fileStat)) == 0)
{
printf("Just entered fstat while loop...\n");
psz = malloc(fileStat.st_size);
printf("Just finished malloc'ing...\n");
}
printf("Entering fgets while loop...\n");
while(fgets(buf, MAXLINE - 1, fpin) != NULL)
{
printf("Inside fgets while loop...\n");
char sub_string1[MAXLINE];
char sub_string2[MAXLINE];
char sub_string3[MAXLINE];
sscanf (buf,"%s %s %s",sub_string1,sub_string2,sub_string3);
printf("Finished sscanf...\n");
printf ("%s\n %s\n %s\n",sub_string1,sub_string2,sub_string3);
}
printf("About to fclose...\n");
fclose(fpin);
printf("File closed...\n");
printf("About to free...\n");
free(psz);
printf("Free finished...\n");
return 0;
}
If you have any other critiques or suggestions on my program I'd be
glad to hear them.
Thanks,
Zach
Then it reads from the stream into the buffer. I am getting a few
warnings. I compiled with very strict options:
gcc -g -W -Wall -std=c99 -pedantic
So I wanted to know if these warnings are something to worry about
(need fixing) or if I can just ignore them:
main5.c: In function ‘main’:
main5.c:30: warning: implicit declaration of function ‘fdopen’
main5.c:30: warning: assignment makes pointer from integer without a
cast
main5.c: At top level:
main5.c:11: warning: unused parameter ‘argc’
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define MAXLINE 90
int main(int argc, char *argv[])
{
char buf[MAXLINE];
int *psz;
int file = 0;
FILE *fpin = {0};
struct stat fileStat;
if((file = open(argv[1], O_RDONLY)) == -1)
{
printf("Cannot open file.\n");
perror("Unable to open sample.dat");
return EXIT_FAILURE;
exit(1);
}
printf("Leaving first if loop...\n");
if((fpin = fdopen(file, "r")) == NULL)
{
printf("Cannot get file descriptor.\n");
perror("fdopen failed");
close(file);
return EXIT_FAILURE;
exit(1);
}
printf("About to call fstat()...\n");
if ((fstat(file,&fileStat)) == 0)
{
printf("Just entered fstat while loop...\n");
psz = malloc(fileStat.st_size);
printf("Just finished malloc'ing...\n");
}
printf("Entering fgets while loop...\n");
while(fgets(buf, MAXLINE - 1, fpin) != NULL)
{
printf("Inside fgets while loop...\n");
char sub_string1[MAXLINE];
char sub_string2[MAXLINE];
char sub_string3[MAXLINE];
sscanf (buf,"%s %s %s",sub_string1,sub_string2,sub_string3);
printf("Finished sscanf...\n");
printf ("%s\n %s\n %s\n",sub_string1,sub_string2,sub_string3);
}
printf("About to fclose...\n");
fclose(fpin);
printf("File closed...\n");
printf("About to free...\n");
free(psz);
printf("Free finished...\n");
return 0;
}
If you have any other critiques or suggestions on my program I'd be
glad to hear them.
Thanks,
Zach