I am trying to run a simulation program to test the FIFO algorithm, however my program is just crashing. this is the main, other functions not shown. Can anyone spot for me the problem.Am not so familiar with using the main Argument[ int main(int argc, char *argv[])]
I have the testing files in a folder
int main(int argc, char *argv[])
{
FILE *stream;
if (argc != 3)
{
printf("The format is: pager file_name memory_size.\n");
//exit(1);
}
printf("File used %s, resident set size %d\n", argv[1], atoi(argv[2]));
if ((stream = fopen(argv[1], "r")) == NULL)
{
perror("File open failed");
//exit(1);
}
mem_size = atoi(argv[2]);
start_simulation(stream);
fclose(stream);
system("pause");
}
Uncomment the calls to exit.
if (argc != 3) {
// insufficient arguments passed..print error and exit.
printf("The format is: pager file_name memory_size.\n");
exit(1);
}
In your case (exit commented) if you don't provide command line arguments, argv[1] will be NULL and this can cause crash when used in fopen
Related
I am unable to save a char array using fprint() and i cannot figure out why. The below codes compliles correctly but saves nothing to file. Please advise.
static char bitSpecial[100];
int main(int argc, char *argv[]) {
FILE *fp
fp = fopen(thefilename, "w+");
if (fp == NULL) {
printf("I couldn't open file for writing.\n");
exit(0);
}
/* populate bitSpecial one character at the time and verify array is full */
fprintf(fp,"%s", bitSpecial);
if (fclose(fp) != 0) puts("Unable to close the file");
return
}
It'll be easier to identify the problem with the full code. I tried the following snippet and it worked:
#include <stdio.h>
#include <string.h> // for strerror
#include <errno.h> // for errno
static char bitSpecial[100];
int main(int argc, char *argv[])
{
char * thefilename = "test";
FILE *fp;
fp = fopen(thefilename, "w+");
if (fp == NULL) {
printf("I couldn't open file for writing.\n");
return 1;
}
/* populate bitSpecial one character at the time and verify array is full */
bitSpecial[0] = 'a';
bitSpecial[1] = '\n';
bitSpecial[2] = '\0'; // terminator
if (fprintf(fp,"%s", bitSpecial) < 0)
printf("[+] fprintf failed with '%s'\n", strerror(errno));
if (fclose(fp) != 0)
puts("Unable to close the file");
return 0;
}
Verify that you put a null terminator (\0) at the end of bitSpecial, and check the return value of fprintf.
I am curious what is the best way to open multiple files. I know you use a combination of FILE *inputfp1; and inputfp1 = fopen(argv[1], "r"); then check for errors. I would like to know the best way to do this.
Is it best to open and close one file at a time like this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv) {
char line[80] = {0};
FILE *inputfp1;
//input = fopen("myfile.txt", "r");
inputfp1 = fopen(argv[1], "r"); //Open file for read.
if (inputfp1 == NULL)
{
printf("Error opening file %s!",argv[1]); //Program prints error message and closes if file is not found
exit(0);
}
if( argc == 7 )
{
/*printf("The first argument supplied is %s\n", argv[1]);
printf("The second argument supplied is %s\n", argv[2]);
printf("The third argument supplied is %s\n", argv[3]);
printf("The first argument supplied is %s\n", argv[4]);
printf("The second argument supplied is %s\n", argv[5]);
printf("The third argument supplied is %s\n", argv[6]);
printf("The third argument supplied is %s\n", argv[7]);*/
}
else if( argc > 7 )
{
printf("Too many arguments supplied.\n");
exit( 1 );
}
else
{
printf("Not enough arguments supplied. \n");
exit( 1 );
}
//Unique behavior on file1
while(fgets(line, 80, inputfp1) != NULL)
{
//do work on file1
}
fclose(inputfp1);
inputfp1 = fopen(argv[2], "r"); //Open file for read.
if (inputfp1 == NULL)
{
printf("Error opening file %s!",argv[1]); //Program prints error message and closes if file is not found
exit(0);
}
//Unique behavior on file2
while(fgets(line, 80, inputfp1) != NULL)
{
//do work on file2
}
fclose(inputfp1);
return 0;
}
Is it better to create all the file pointers and open all the files at once like this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv) {
char line[80] = {0};
FILE *inputfp1;
FILE *inputfp2;
FILE *inputfp3;
FILE *inputfp4;
FILE *inputfp5;
FILE *inputfp6;
//input = fopen("myfile.txt", "r");
inputfp1 = fopen(argv[1], "r"); //Open file for read.
inputfp2 = fopen(argv[2], "r"); //Open file for read.
inputfp3 = fopen(argv[3], "r"); //Open file for read.
inputfp4 = fopen(argv[4], "r"); //Open file for read.
inputfp5 = fopen(argv[5], "r"); //Open file for read.
inputfp6 = fopen(argv[6], "r"); //Open file for read.
if (inputfp1 == NULL)
{
printf("Error opening file %s!",argv[1]); //Program prints error message and closes if file is not found
exit(0);
}
//The rest of error checking.
if( argc == 7 )
{
/*printf("The first argument supplied is %s\n", argv[1]);
printf("The second argument supplied is %s\n", argv[2]);
printf("The third argument supplied is %s\n", argv[3]);
printf("The first argument supplied is %s\n", argv[4]);
printf("The second argument supplied is %s\n", argv[5]);
printf("The third argument supplied is %s\n", argv[6]);
printf("The third argument supplied is %s\n", argv[7]);*/
}
else if( argc > 7 )
{
printf("Too many arguments supplied.\n");
exit( 1 );
}
else
{
printf("Not enough arguments supplied. \n");
exit( 1 );
}
//Unique behavior on file1
while(fgets(line, 80, inputfp1) != NULL)
{
//do work on file1
}
fclose(inputfp1);
//Unique behavior on file2
while(fgets(line, 80, inputfp2) != NULL)
{
//do work on file2
}
fclose(inputfp2);
//The rest of reading and closing files.
return 0;
}
Are there any better ways I missed?
A good way of doing this would be putting all your file pointers in an array:
FILE *inputfp[6];
for(int i=0;i<6;i++)
{
inputfp[i] = fopen(argv[i+1], "r"); //Open file for read.
if (inputfp[i] == NULL)
{
printf("Error opening file %s!",argv[i+1]); //Program prints error message and closes if file is not found
exit(0);
}
}
I'd do it the first way, but use a loop:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv) {
char line[80] = {0};
FILE *inputfp1;
if( argc == 7 )
{
/*printf("The first argument supplied is %s\n", argv[1]);
printf("The second argument supplied is %s\n", argv[2]);
printf("The third argument supplied is %s\n", argv[3]);
printf("The first argument supplied is %s\n", argv[4]);
printf("The second argument supplied is %s\n", argv[5]);
printf("The third argument supplied is %s\n", argv[6]);
printf("The third argument supplied is %s\n", argv[7]);*/
}
else if( argc > 7 )
{
printf("Too many arguments supplied.\n");
exit( 1 );
}
else
{
printf("Not enough arguments supplied. \n");
exit( 1 );
}
for (int i = 1; i < argc; ++i)
{
inputfp1 = fopen(argv[i], "r"); //Open file for read.
if (inputfp1 == NULL)
{
printf("Error opening file %s!",argv[i]); //Program prints error message and closes if file is not found
exit(0);
}
while(fgets(line, 80, inputfp1) != NULL)
{
//do work
}
fclose(inputfp1);
}
return 0;
}
I'm writing code that's supposed to verify that a .txt file is a certain format.
I wrote my code as I saw in a tutorial and in the website
and for some reason my program doesn't even print my file.
Can you tell me what I'm doing wrong?
The code will do something far more complex, but I'm still trying to work on my basics.
Here's my code so far:
int main(int argc, char *argv[]) {
/* argv[0] = name of my running file
* argv[1] = the first file that i receive
*/
define MAXBUFLEN 4096
char source[MAXBUFLEN + 1];
int badReturnValue = 1;
char *error = "Error! trying to open the file ";
if (argc != 2) {
printf("please supply a file \n");
return badReturnValue;
}
char *fileName = argv[1];
FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */
if (fp != NULL) {
size_t newLen = fread(&source, sizeof(char), MAXBUFLEN, fp);
if (ferror(fp) != 0) {
printf("%s %s", error, fileName);
return badReturnValue;
}
int symbol;
while ((symbol = getc(fp)) != EOF) {
putchar(symbol);
}
printf("finish");
fclose(fp);
}
else {
printf("%s %s", error, fileName);
return badReturnValue;
}
}
I think you need a bit more explanations:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// there might be a macro BUFLEN defined in stdio
// which size is optimized for reading in chunks.
// Test if avaiable otherwise define it
#ifndef BUFLEN
# define BUFLEN 4096
#endif
int main(int argc, char *argv[])
{
char source[BUFLEN];
char *filename;
FILE *fp;
size_t fpread, written;
char c;
int ret_fclose;
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
// reset errno, just in case
errno = 0;
// work on copy
filename = malloc(strlen(argv[1]) + 1);
if (filename == NULL) {
fprintf(stderr, "Allocating %zu bytes failed\n", strlen(argv[1]) + 1);
exit(EXIT_FAILURE);
}
filename = strcpy(filename, argv[1]);
// try to open the file at 'filename'
fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Opening file \"%s\" filename failed\n", filename);
// errno might got set to something usable, check and print
if (errno != 0) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
// You have two options here. One is to read in chunks of MAXBUFLEN
while ((fpread = fread(&source, 1, BUFLEN, fp)) > 0) {
// Do something with the stuff we read into "source"
// we do nothing with it here, we just write to stdout
written = fwrite(&source, 1, fpread, stdout);
// you can use 'written' for error check when writing to an actual file
// but it is unlikely (but not impossible!) with stdout
// test if we wrote what we read
if ((fpread - written) != 0) {
fprintf(stderr, "We did not write what we read. Diff: %d\n",
(int) (fpread - written));
}
}
// fread() does not distinguish between EOF and error, we have to check by hand
if (feof(fp)) {
// we have read all, exit
puts("\n\n\tfinish\n");
// No, wait, we want to do it again in a different way, so: no exit
// exit(EXIT_SUCCESS);
} else {
// some error may have occured, check
if (ferror(fp)) {
fprintf(stderr, "Something bad happend while reading \"%s\"\n", filename);
if (errno != 0) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
}
// the other way is to read it byte by byte
// reset the filepointers/errors et al.
rewind(fp);
// rewind() should have reseted errno, but better be safe than sorry
errno = 0;
printf("\n\n\tread and print \"%s\" again\n\n\n\n", filename);
// read one byte and print it until end of file
while ((c = fgetc(fp)) != EOF) {
// just print. Gathering them into "source" is left as an exercise
fputc(c, stdout);
}
// clean up
errno = 0;
ret_fclose = fclose(fp);
// even fclose() might fail
if (ret_fclose == EOF) {
fprintf(stderr, "Something bad happend while closing \"%s\"\n", filename);
if (errno != 0) {
fprintf(stderr, "Error: %s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
// The macros EXIT_FAILURE and EXIT_SUCCESS are set to the correct values for
// the OS to tell it if we had an eror or not.
// Using exit() is noot necessary here but there exits teh function atexit()
// that runs a given function (e.g: clean up, safe content etc.) when called
exit(EXIT_SUCCESS);
}
You read from the file twice but only print once.
If the file is to small the first reading will read all of the contents, and the second reading will not produce anything so you don't print anything.
I believe you have to reset the pointer after using fread.
Try fseek(fp, SEEK_SET, 0) to reset the pointer to the beginning of the file. Then print the file.
I am having the toughest time with this assignment. So this assignment I have two children(two separate programs) and they have to write to the parent (main). The parent has to read both data from the kids and then print it out. I have to use named pipes. Well so my FIFO keeps giving me "USAGE: NAMEPIPECLIENT[String]" message and I don't know why. The message is on the client side by the way. Also if someone can point me in a good direction on how to use fork with multiply children on separate files that would be really appreciated.
Thanks in advance!Using GNU C
My Reader
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO" //default is current directory
int main(void){
FILE *fpoint;
char readbuffer[80];
int again = 1;
mknod(FIFO_FILE, S_IFIFO | 0666, 0);
while(again){
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
return(0);
}//exit main
}
My Writer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[]){
FILE *fpoint;
int again =1;
char strIn[80] = "Use message from command line";
if(argc !=2){
printf("USAGE: NamedPipeClient[string]\n");
exit(1);
}
strcpy(strIn, argv[1]);
while(again == 1){
if((fpoint = fopen (FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
fputs(strIn, fpoint);
fclose(fpoint);
printf("Enter message to send: ");
scanf("%s", strIn);
again = strcmp(strIn, "Stop");
}
if((fpoint = fopen(FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
fputs(strIn,fpoint);
fclose(fpoint);
return(0);
}
Here is an extensively corrected version of the writer process
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//#include<sys/stat.h>
//#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[])
{
if(argc !=2)
{
printf("USAGE: NamedPipeClient[string]\n");
exit(1);
}
FILE *fpoint;
if((fpoint = fopen (FIFO_FILE, "w")) == NULL)
{
perror("fopen");
exit(1);
}
char strIn[80];
strcpy(strIn, argv[1]);
int again =1;
while(again == 1)
{
fputs(strIn, fpoint);
printf("Enter message to send: ");
scanf("%79s", strIn);
again = strcmp(strIn, "Stop");
}
fputs(strIn,fpoint);
fclose(fpoint);
return(0);
}
This is the main reason that only one string is read:
while(again)
{
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
return(0);
}
Note that the function returns after only one pass through the loop.
Suggest:
while(again)
{
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
}
return 0;
Note: return is not a function, (similar to sizeof is not a function), so no parens needed.
Note: the continually opening and closing of the FIFO is not a good idea.
Suggest only open once in any one process.
Suggest only close once in any one process.
When calling fopen(), always check the returned value to assure the operation was successful.
I have the following code which reads an file name from the command line and opens this file:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
FILE *datei;
char filename[255];
//filename = argv[1];
//datei=fopen(filename, "r");
datei=fopen(argv[1], "r");
if(datei != NULL)
printf("File opened");
else{
printf("Fehler beim öffnen von %s\n", filename);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
This example works, but I want to write the string from the command line to the char array and pass that char array to to fopen(), but i get the compiler error
Error: assignment to expression with array type filename = argv[1];
What does this error mean and what can I do to fix it?
You must copy the string into the char array, this cannot be done with a simple assignment.
The simplistic answer is strcpy(filename, argv[1]);.
There is a big problem with this method: the command line parameter might be longer than the filename array, leading to a buffer overflow.
The correct answer therefore:
if (argc < 2) {
printf("missing filename\n");
exit(1);
}
if (strlen(argv[1]) >= sizeof(filename)) {
printf("filename too long: %s\n", argv[1]);
exit(1);
}
strcpy(filename, argv[1]);
...
You might want to output the error messages to stderr.
As a side note, you probably want to choose English or German, but not use both at the same time ;-)
An even simpler solution is to just keep a copy of the pointer argv[1] in a char *filename. Unless you modify it yourself, a very bad idea, its contents will not change for the duration of the program execution.
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
FILE *datei;
char *filename;
if (argc < 2) {
fprintf(stderr, "Fehlendes Dateiname-Befehlszeilenargument\n");
return EXIT_FAILURE;
}
filename = argv[1];
datei = fopen(filename, "r");
if (datei != NULL) {
printf("Datei erfolgreich geöffnet\n");
} else {
fprintf(stderr, "Fehler beim öffnen von %s: %s\n",
filename, strerror(errno));
return EXIT_FAILURE;
}
// ...
fclose(datei);
return EXIT_SUCCESS;
}