Cannot open file with fopen(); function - C Programing [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to create a program that using the fopen(); function.
My problem is that fopen(); cannot find the file.
when i am using perror("Error"); the output is Error: No such file or directory.
I have read this articles and they did`not solved my problem:
fopen() returning a NULL pointer, but the file definitely exists
Unable to open a file with fopen()
It means i have tried to use fopen(); with the exec path to the file, and i have checked that the filename is the specific filename that i am looking for.
I have also used exec("pwd"); to see that i am the correct directory.
My OS is ubuntu mate 20.04.
Here is my Code:
/* File Name: firstTransaction.c
* File Mission: scan the input files and make the first transaction.
*/
#include "defines.h"
/*
* first transaction file:
* Allocating new memory for the assembly source file.
* Scanning the assembly source file.
* #param filename - the filename of the file that needs to be converted by the program.
* #return output and files -
* output the assembly source file in a machine code.
* return extern, entry and object files.
*/
/* function prototype */
extern char * readText(FILE * ptr, long ic, long dc); /* the function defined on assistanceFunctions.c file */
int firstTransaction(int * cf, char **av)
{
FILE * fp; /* a pointer for fopen function */
char * filteredFile = NULL;
long ic = 100, dc = 0; /* declaration and initialization of the instruction counter and current data counter */
int i = 0, j = 0; /* indexes */
int cfh = 0; /* compatible file holder */
int fileNameLength = (int)strlen(av[i]); /* computing the first filename length to allocate memory */
char * fileName = (char*) calloc(fileNameLength,sizeof (char)); /* allocating memory for the first filename */
system("pwd");
while(cf[i] != 0) /* while there is more compatible files to open */
{
cfh = cf[i]; /* cfh - compatible file holder, cf - compatible file array, set the next compatible file to cfh */
strcpy(fileName,av[cfh]); /* copy the file name from the argv array */
fp = fopen("fileName","r"); /* open the first compatible file for read */
if(fp == NULL) /* if file does not exists */
{
perror("Error"); /* print the error - why the file not opened */
i++; /* increment i by one and try to open the next filename */
}else /* if the file was opened successfully, start scanning the file */
{
/*filteredFile = */ readText(fp, ic, dc); /* calling readText function with pointer to the start of the file that was opend by fopen function */
/* while(filteredFile[j] != EOF)
{
putchar(filteredFile[j]);
j++;
}
/* DONT FORGET TO CLOSE WITH FCLOSE(); */
}
}
return 0;
}
Here is some screenshots with the tested solutions that failed from the other articles:
Edited after NeonFire answers:
My first code used fp = fopen(fileName,"r"); but my error message was made manually this way: printf("error, %s file does not exists\n", fileName); /* print error message to the user */
this cause that i did`not find my real error.
then i changed to fp = fopen("fileName","r"); and used perror("Error"); instead of the first and right way.
after i read NeonFire answers i got a new error Error: Too many open files.
i removed the backups file from the directory and i still have the same error.
Edited after Ted Lyngmo comment:
i did had to close the file to solve the - "to many file to open error".

your fp = fopen("fileName","r"); is incorrect. Remove the quotes from fileName so it refers to your variable char * fileName , not a string "fileName" :)

Related

How to replace the text into a particular location of a file by passing the argument via command line argument

My intention is to read the second element in the 1st row and replace it with the value which we pass as an command line argument and replace it in the input.txt file
Input file:logic.txt
one=1234
two=3456
I want my file to be changed like this after compiling the code.
./a.out 1567
Currently i am getting output like this
./filelogic 1567
1567=1234
two=5678
Expected Output file should be modified like this after the compilation
logic.txt
one=1567
two=5678
char buf[MAXSIZE] = {};
int num = 0;
int i = 0;
num = atoi(argv[1]);
printf("%d",num);
FILE *fp1;
fp1 = fopen("logic.txt","r");//currently reading the file.
if(fp1 != NULL)
{
fseek(fp1,3,0);//Need to Move the pointer to the 3rd position where i need to replace the num(fseek(fp1,??,0))-->how we can achieve that.
//Using which method i can replace the num value into a file (means need to replace 1234 inplace of 1567)
//Once the changes are done need to replace in the same file.
fread(buf, 1, MAXSIZE, fp1);
printf("%s\n",buf);
fclose(fp1);
}else {
printf("Cannot open file"");
exit(1);
}
Could someone guide me to resolve this issue?Thanks in advance
You can make replacements to a file in-place, but you should not do this in practice. You will likely corrupt the file if you attempt to replace characters and do not make an exact one-to-one replacement of characters already in the file.
To safely change the contents of a file, read the entire file contents into memory, make the changes needed, and then truncate the current file and write the new contents to the truncated file. (if the file is too large for in-memory operations, then use a temporary file)
You do not want to use atoi to convert the string "1567" to an integer. You are replacing characters in a file, not integer values in a binary file, so work with characters instead.
Your project is complicated by only wanting to replace the text after the first '=' sign. This may or may not be on the first line of the file, so you will need some flag to indicate when the first '=' is found and the replacement is made. (once the replacement is made, you can simply break your read loop and close the file -- but below the example output all lines for convenience)
Any time you close a file after writing to it, you should validate the return of fclose to catch any stream errors, or errors that occurred on the last write that will not be apparent until the next file operation.
With those cautions and caveats in mind, you could do something similar to:
#include <stdio.h>
#include <string.h>
#define MAXSIZE 64 /* max line/buffer size */
#define FNAME "logic.txt" /* default file name */
#define REPLACE "1567" /* default replacement text */
int main (int argc, char **argv) {
char buf[MAXSIZE] = ""; /* line buffer */
const char *str = argc > 1 ? argv[1] : REPLACE; /* replacement string */
int replaced = 0; /* flag indicating replacement made */
FILE *fp = fopen (FNAME, "r+"); /* open file reading/writing */
if (!fp) { /* validate file open for reading/writing */
perror ("fopen-FNAME");
return 1;
}
while (fgets (buf, MAXSIZE, fp)) { /* read each line in file */
if (!replaced) { /* if replacement not yet made */
char *p = strchr (buf, '='); /* search for '=' in line */
if (p) { /* if found */
size_t plen = 0; /* var for length to end */
p++; /* advance past '=' sign */
plen = strlen (p); /* get length to end */
if (plen < strlen (str)) { /* check avail length */
fprintf (stderr, "error: not enough space in line.\n");
return 1;
}
strcpy (p, str); /* copy str to p */
if (fseek (fp, -plen, SEEK_CUR)) { /* backup plen chars */
perror ("fseek(fp)");
return 1;
}
fputs (p, fp); /* overwite contents with replacement */
replaced = 1; /* set flag indicating replacement */
} /* (you can break, and remove output */
} /* here if not writing to stdout) */
fputs (buf, stdout); /* output lines to stdout (optional) */
}
if (fclose (fp) == EOF) /* always validate close-after-write */
perror ("fclose-FNAME");
return 0;
}
Using your file logic.txt as an example input, and naming the executable filelogic as you have, the use and operation of the code above yields:
logic.txt File Before
$ cat logic.txt
one=1234
two=3456
Example Use/Output
$ ./filelogic
one=1567
two=3456
logic.txt File After
$ cat logic.txt
one=1567
two=3456
Again, this is fine for a learning endeavor, but in practice, avoid making changes to files in-place as the risk of inadvertent file corruption far outweighs writing a new file with the changes.

Quickest way to check whether or not file exists [duplicate]

This question already has answers here:
What's the best way to check if a file exists in C?
(8 answers)
Closed 5 years ago.
The way I'm using just involves trying to fopen() the file to be checked,
/* --- does file exist??? --- */
char fname[999] = "whatever"; /* constructed during execution */
FILE *fp = NULL; /* try to fopen(fname,"r") */
int isfilefound = 0; /* set true if fopen() succeeds */
if ( (fp = fopen(fname,"r")) /* try to fopen() for read */
!= NULL ) { /* succeeded */
isfilefound = 1; /* set file found flag */
fclose(fp); } /* and just close the file */
Is there a quicker, less resource-intensive, way?... A specific way for unix/linux? A Windows way? And preferably, a portable posix-compliant way (as above presumably is)? It's being done lots (1000's) of times, so I'd prefer not to be unnecessarily opening and closing files for no good reason.
-----------------------------------------------------------------
Edit Okay, based on answers below, I put together the following little function intended to check whether or not file (already:) exists in a posix,windows,other portable way...
/* ==========================================================================
* Function: isfilexists ( path )
* Purpose: check whether file at path exists
* --------------------------------------------------------------------------
* Arguments: path (I) pointer to null-terminated char string
* containing "path/filename.ext" of
* file whose existence is to be determined
* (path is relative to pwd unless explicitly
* absolute by initial '/' or other syntax)
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if file at path exists, or 0 if not
* --------------------------------------------------------------------------
* Notes: o conditional compiles for various systems,
* depending on whether POSIX or WINDOWS is #define'ed...
* o ...method used:
* 1: use access() on Posix systems,
* 2: PathFileExists() on Windows systems,
* 3: fopen() on any other systems.
* ======================================================================= */
/* --- entry point --- */
int isfilexists ( char *path )
{
/* ---
* allocations and declarations
* ------------------------------- */
int isexists = 0; /* set true if file at path exists */
FILE *fp = NULL; /* fopen() for non-posix,windows */
#define POSIX /* just for testing */
/* ---
* determine whether file at path already exists
* ------------------------------------------------ */
#if defined(POSIX) /* posix-compliant system... */
#include <unistd.h>
if ( access(path,F_OK) == 0 ) /* file at path exists */
isexists = 1; /* so set file exists flag */
#else
#if defined(WINDOWS) /* Windows system... */
isexists = PathFileExists(path); /* set flag if file at path exists */
#else
/* --- fopen() for any other non-posix, non-windows system --- */
if ( (fp = fopen(path,"r")) /* try to fopen() for read */
!= NULL ) { /* succeeded */
isexists = 1; /* set file exists flag */
fclose(fp); } /* and just close the file */
#endif
#endif
return ( isexists ); /* back to caller with 1 if file at path exists */
} /* --- end-of-function isfilexists() --- */
The access() and fopen() methods tested and work okay. Unable to test PathFileExists() for windows. And I still want to figure out what #define'ed symbols to automatically and unambiguously check for conditional compiles.
You are thinking about the problem the wrong way. You shouldn't ever "check whether a file already exists", because that has an inherent TOCTOU race — in between the time you check whether the file exists, and the time you act on that information, another process may come along and change whether the file exists, rendering the check invalid.
What you do instead depends on why you want to know. One very common case is that you only want to create the file if it doesn't already exist, in which case you use the lower-level open function in O_EXCL mode:
int fd = open("whatever", O_WRONLY|O_CREAT|O_EXCL, 0666);
if (fd == -1 && errno == EEXIST) {
/* the file already exists */
} else if (fd == -1) {
/* report that some other error happened */
} else {
FILE *fp = fdopen(fd, "w");
/* write data to fp here */
}
Another very common case is that you want to create the file if it doesn't exist, or append new data to the file if it does; this can be done with the "a" mode to fopen or O_APPEND flag to open.
On Windows, there is PathFileExists().
On a POSIX system, you have stat() or access().
That said, if you check for existence of the file because your code needs the file, this is the wrong approach -- file systems are out of your program's control, so this would be a race condition, the only correct way would be to properly handle errors when opening the file.

C fopen() - Possible issue with absolute path [duplicate]

This question already has answers here:
Returning a string from function
(3 answers)
Closed 7 years ago.
I'm currently using Code::Blocks 13.12 (compiler: GNU GCC) under Windows 10.
I'm trying to open a file and load its content, but fopen gives me troubles. The 'input.txt' exists in the same directory as my executable. I've already checked the permissions.
Function for getting the path:
char* getFileName()
{
char *fileName; /* the path of the .txt file */
char path[MAX_PATH];
/* get the path of the executable */
GetModuleFileName(NULL, path, MAX_PATH);
/* remove the name of the executable from the path */
PathRemoveFileSpec(path);
/* check case where path is directory root */
if(PathIsRoot(path))
strcat(path, "\\*");
/* add the name of the .txt file to the path */
strcat(path, "\\input.txt");
/* not sure if needed */
path[strlen(path)] = '\0';
fileName = strdup((char *) path);
return fileName;
}
Function for loading the contents of the file:
bool loadDict(char *fileName)
{
FILE *fp; /* file stream */
char line[LINE_SIZE]; /* each line of the file */
// other variables
/* try to open file for reading */
if((fp = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Error: Could not open the file '%s' to read\n", fileName);
return false;
}
// stuff done
/* file is no longer needed, close it */
if(fclose(fp))
{
fprintf(stderr, "Error: Could not close the file '%s' to read\n", fileName);
return false;
}
return true; /* in case no problem has occured */
}
Main:
int main()
{
char *fileName;
fileName = getFileName();
/* try to load the dictionary into memory */
if(!loadDict(fileName))
{
fprintf(stderr, "Error: The dictionary could be not loaded into memory.\nProgram terminating...\n");
return 1;
}
// other stuff
return 0;
}
I'm getting both errors (could not open the file, could not load). I've already tried replacing '\' with '/' or using double slashes without success.
FILE *fp = fopen("path\\input.txt", "r");
Any help would be appreciated.
You're returning the address of a local variable in getFileName, which results in undefined behavior. This is a common pitfall in C.
You need to either: A) Allocate the string on the heap (using e.g. malloc) and return it.B) Have getFileName take a pointer to a caller-allocated buffer which it then populates.
Also, when debugging problems like this, don't just assume everything is working. Use printf to see what the value of filename is before you try to fopen it.
Your array path is a local variable whose scope is limited to function getFileName. Don't return its address .
Instead pass it from the calling function.

How to write an array to file in C

I have a 2 dimensional matrix:
char clientdata[12][128];
What is the best way to write the contents to a file? I need to constantly update this text file so on every write the previous data in the file is cleared.
Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode:
FILE *f = fopen("client.data", "wb");
fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
fclose(f);
This writes out the whole 2D array at once, writing over the content of the file that has been there previously.
I would rather add a test to make it robust !
The fclose() is done in either cases otherwise the file system will free the file descriptor
int written = 0;
FILE *f = fopen("client.data", "wb");
written = fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
if (written == 0) {
printf("Error during writing to file !");
}
fclose(f);
How incredibly simple this issue turned out to be...
The example given above handle characters, this is how to handle an array of integers...
/* define array, counter, and file name, and open the file */
int unsigned n, prime[1000000];
FILE *fp;
fp=fopen("/Users/Robert/Prime/Data100","w");
prime[0] = 1; /* fist prime is One, a given, so set it */
/* do Prime calculation here and store each new prime found in the array */
prime[pn] = n;
/* when search for primes is complete write the entire array to file */
fwrite(prime,sizeof(prime),1,fp); /* Write to File */
/* To verify data has been properly written to file... */
fread(prime,sizeof(prime),1,fp); /* read the entire file into the array */
printf("Prime extracted from file Data100: %10d \n",prime[78485]); /* verify data written */
/* in this example, the 78,485th prime found, value 999,773. */
For anyone else looking for guidance on C programming, this site is excellent...
Refer: [https://overiq.com/c-programming/101/fwrite-function-in-c/

How can I get a file pointer ( FILE* ) of known file in a specific path?

to read/write a file i need file pointer in language C in Unix environment.
I know a file name and path, but how to get file pointer using its name and path.
#include <stdio.h>
FILE * pFile;
pFile = fopen ("myfile.txt","w");
Import the standard input/output header like so
#include <stdio.h>
And then create a pointer for the file you want to open.
FILE * file_pointer;
file_pointer = fopen ("[path to file]","w");
fclose(file_pointer);
NOTE: Specify whole path to file if it is not in the same directory as your source file.
Dont forget to close the file after you have done the operations you need
According to the post from ssmithstone:
#include <stdio.h>
FILE * pFile;
/* open file and check if was successful */
if ((pFile = fopen("myfile.txt", "w")) == NULL)
{
/* couldn't open file; do some error handling if u want */
}
else
{
/* do s.th. */
/* close file */
fclose(pFile);
}
In this case w means writing. For other options check the link posted by Yu Hao.
Seems like you are new in C programming, I've written a C program for you, you can analyse it and I believe it will be definitely helpful to you.
#define size 50
#include <stdio.h>
int main()
{
char name[size];
FILE *file;
file = fopen("your_file.txt","w");
printf("Please enter your first name\n");
scanf("%s",name);
fprintf(file,"%s",name);
fclose(file);
return 0;
}
Details:
In line 7 the second parameter w is used as file open mode - with write privileges.
The file pointer is used to create / open a file with name "your_file.txt".
function fprintf() is same as printf() function but it writes not on console but to your file.
finally we need to close the file writing operations thus we use fclose() function
Update:
To specify your path you can write your file path with your filename.fileextension
for example: You can write it as
file = fopen("/home/depthgr8/Desktop/your_file.txt","w");
This will create your_file.txt in given path if your path exists otherwise it will throw a runtime exception - segmentation fault (core dumped)

Resources