I am having trouble compiling my program.
The error message is: undefined reference to `_fcloseall', I think it could be a missing library file at the beginning. It might be also useful to know that I am programming on Windows 8.1 + Cygwin. Which library could be missing or do you see any other mistake?
Here is the code:
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void cleanup1();
void cleanup2();
int main(int argc, char *argv[])
{
FILE * file;
if(argc < 2){
printf("\ncommand bsp10085 <file>");
exit(1);
}
assert(atexit(cleanup1) == 0);
assert(atexit(cleanup2) == 0);
if((datei = fopen(argv[1], "r")) != NULL){
printf("\nfile %s is being processed ..",argv[1]);
fclose(datei);
}
else
printf("\nfile '%s' is missing. ", argv[1]);
}
void cleanup1(){
printf("\nCleanup the rest");
}
void cleanup2(){
printf("\nClose all open files");
fflush(NULL);
_fcloseall();
}
I tried to compile your code (in ubuntu though) and i also got a warning: warning: implicit declaration of function ‘fcloseall’ [-Wimplicit-function-declaration] fcloseall();
I think that if you add
#define _GNU_SOURCE
before
#include<stdio.h>
your program should work fine. This is your code after i changed some other warnings as well:
#define _GNU_SOURCE
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
void cleanup1();
void cleanup2();
int main(int argc, char *argv[])
{
FILE *datei;
if(argc < 2){
printf("\ncommand bsp10085 <file>");
exit(1);
}
assert(atexit(cleanup1) == 0);
assert(atexit(cleanup2) == 0);
if((datei = fopen(argv[1], "r")) != NULL){
printf("\nfile %s is being processed ..",argv[1]);
fclose(datei);
}
else
printf("\nfile '%s' is missing. ", argv[1]);
return 0;
}
void cleanup1(){
printf("\nCleanup the rest");
}
void cleanup2(){
printf("\nClose all open files");
fflush(NULL);
fcloseall();
}
Related
My program check if two files (with a different path / name) match or not. The hard and symbolic links between the files are followed to determine it.
How can I modify the program to see what happens if the files I want to compare are device files? Or directories? Both seem valid uses. Also checking the device
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
void printUsage()
{
printf("Use: ./leg <name_file1> <name_file2>.\n");
}
int createStat(const char *file, struct stat *fileStat)
{
if (stat(file, fileStat) < 0)
{
fprintf(stderr,"Error! File %s does not exist.\n", file);
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
if (argc < 3)
{
printf("Insufficient number of parameters.\n");
printUsage();
return -1;
}
struct stat fileStat1;
struct stat fileStat2;
if (createStat(argv[1], &fileStat1) == 0)
{
return -1;
}
if (createStat(argv[2], &fileStat2) == 0)
{
return -1;
}
if (S_ISREG(fileStat1.st_mode) && S_ISREG(fileStat2.st_mode)) {
if ((fileStat1.st_dev == fileStat2.st_dev) && (fileStat1.st_ino == fileStat2.st_ino)) {
printf("Files '%s' and '%s' coincide.\n", argv[1], argv[2]);
} else
printf("Files '%s' and '%s' do not match.\n", argv[1], argv[2]);
} else
fprintf(stderr,"'%s' and '%s' there are no files.\n", argv[1], argv[2]);
return 0;
}
For regular files and directories, comparing the st_dev and st_ino fields is sufficient.
For paths representing character or block devices, comparing the st_dev and st_ino fields will tell you if they are the same file, ie: different paths to the same directory entry, including symbolic link indirections. Comparing the st_rdev fields will tell if they represent the same device, which is also useful but a different thing.
Also note that fprintf(stderr,"Error! File %s does not exist.\n", file); may produce a misleading message: access failure may be caused by other reasons. It is simple and efficient to produce the correct message this way:
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int createStat(const char *file, struct stat *fileStat) {
if (stat(file, fileStat) < 0) {
fprintf(stderr, "Cannot stat file %s: %s\n", file, strerror(errno));
return 0;
}
return 1;
}
I created a C program which would create a directory and file.
I have tried to debug the error, but it didn't work
#include <dirent.h>
#include <errno.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
create_dir(char* outputdir,char* str_outpath,char* value){
DIR* dir = opendir(outputdir);
FILE *f;
if (dir) {
/* Directory exists. */
closedir(dir);
} else if (ENOENT == errno) {
/* Directory does not exist. */
mkdir(outputdir, 0700);
closedir(dir);
printf("Successfully created the directory %s ", outputdir);
} else {
printf("Creation of the directory %s failed",outputdir);
/* opendir() failed for some other reason. */
}
f = fopen(str_outpath, "a");
fprintf(f,"%s",value);
fclose(f);
}
I want it to create a file and a directory successfully
As others have mentioned. You do not have a main function.
Also your create_dir function is missing a type. I'll assume it's void since you are not returning anything. This should compile.
#include <dirent.h>
#include <errno.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
void create_dir(char* outputdir,char* str_outpath,char* value){
DIR* dir = opendir(outputdir);
FILE *f;
if (dir) {
/* Directory exists. */
closedir(dir);
} else if (ENOENT == errno) {
/* Directory does not exist. */
mkdir(outputdir, 0700);
closedir(dir);
printf("Successfully created the directory %s ", outputdir);
} else {
printf("Creation of the directory %s failed",outputdir);
/* opendir() failed for some other reason. */
}
f = fopen(str_outpath, "a");
fprintf(f,"%s",value);
fclose(f);
}
int main(){
char directory[] = "/users/me/documents/testdir";
char filepath[] = "testfile";
char data[] = "hello world";
create_dir(directory,filepath,data);
return 0;
}
I did not execute the code to check whether it works. I merely copied and pasted yours and called the function.
In C under most cases you need to have a main function. So in order to run your code you'll need to have something like this (assuming that you want to pass in the parameters from the command-line) underneath that function:
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Proper Usage is ./program otputdir str_outpath value\n");
return -1;
}
char *outputdir = argv[1];
char *str_outpath = argv[2];
char *value = argv[3];
create_dir(outputdir, str_outpath, value);
return 0;
}
EDIT: fixed an issue with not checking argc
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;
}
I am having a problem with the below code validating a file using regex. My file must only contain letters or numbers. My regular expression is:
#define to_find "^[a-zA-Z0-9]+$"
which is located in my main.h file. The below code is in my main.c
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "main.h"
int main(int argc, char * argv[])
{
int ret_val;
regex_t regex;
FILE *fp;
char line[1024];
if (regcomp(®ex, to_find, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
return EXIT_FAILURE;
}
if (argc > 2)
{
printf("Usage: tree OR tree [filename]\n");
return EXIT_FAILURE;
}
else if (argc == 2)
{
fp = fopen(strcat(argv[1],".dat"), "r");
printf("file opened\n");
while ((fgets(line, 1024, fp)) != NULL)
{
line[strlen(line) - 1] = '\0';
if ((ret_val = regexec(®ex, line, 0, NULL, 0)) != 0);
{
printf("Error: %s\n", line);
return EXIT_FAILURE;
}
}
fclose(fp);
printf("File closed\n");
}
return 0;
}
My file I am reading is called names.dat and contains:
int
char
[
double
What is happening is it is kicking out at the very first line which it should kick out at the 3rd line. I am sure this is pretty simple to solve but it seems I have not figured it out. I would appreciate any help. Also, how do I deal with the
\n
character in the file? this file will have several lines. Thanks in advance.
You have some small errors but the one that cause the error is:
// Do you see this sweet little semicolon :P ----------------+
if ((ret_val = regexec(®ex, line, 0, NULL, 0)) != 0); // <+
{
printf("Error: %s\n", line);
return EXIT_FAILURE;
}
beside this line:
fp = fopen(strcat(argv[1],".dat"), "r");
You cannot add to argv, you need to create a new buffer to hold the data, create a buffer with PATH_MAX size add append the path to it. Here an improved version:
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#define to_find "^[a-zA-Z0-9]+$"
int main(int argc, char * argv[])
{
int ret_val;
regex_t regex;
FILE *fp;
char file[PATH_MAX];
char line[1024];
if (regcomp(®ex, to_find, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
return EXIT_FAILURE;
}
if (argc > 2)
{
printf("Usage: tree OR tree [filename]\n");
return EXIT_FAILURE;
}
else if (argc == 2)
{
sprintf(file, "%s.dat", argv[1]);
fp = fopen(file, "r");
if( fp == NULL ) {
perror("Error");
return -1;
}
printf("file opened\n");
while (fscanf(fp, "%1023s", line) > 0)
{
if ((ret_val = regexec(®ex, line, 0, NULL, 0)) != 0)
{
printf("Not match: %s\n", line);
//return EXIT_FAILURE;
} else {
printf("Match: %s\n", line);
}
}
regfree(®ex);
fclose(fp);
printf("File closed\n");
}
return 0;
}
See the diff: http://www.diffchecker.com/8itbz5dy
test:
$ gcc -Wall sample.c
$
$ cat name.dat
int
char
[
double
$ ./a.out name
file opened
Match: int
Match: char
Not match: [
Match: double
File closed
$