C fopen() unable to open file in /tmp - c

I am following the basic C programming tutorial on tutorialspoint.com
I have the following program which generates a file in /tmp called test.txt:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("/tmp/test.text", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;
}
Then I have a second program which just tries to open that file for reading:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
FILE *fp = NULL;
fp = fopen("/tmp/test.txt", "r");
if (fp == NULL) {
printf("NULL!!!\n");
}
printf("%s\n", strerror(errno));
fclose(fp);
return 0;
}
However, when I try to run the program that opens the file, I get the following output:
NULL!!!
No such file or directory
Segmentation fault
If I modify the code to point to the same file in my home directory, it works correctly. It seems that, for some reason, I am not able to open files in the /tmp directory (via fopen)... And just to be clear, I am able to change to /tmp and cat the contents of the test.txt file just fine. Permissions look normal on it as well, 664 with my user as the owner and group.
The only other specifics that I can think of that might have to do with my system is that I am on Elementary OS Juno, I am using g++ 7.3.0 (clang also gives the same result), and I have separate encrypted partitions for my OS root and home...
Any thoughts on what might be causing this?

This was simply caused by an incorrect file extension, .txt vs.text as pointed out by #yano

Related

How to write something in a existing .txt file with c program?

I am trying to write "File opened" in a existing test.txt file with C program. But I must not create a test.txt file with the program. I have to write with in a existing file.
I tried but can't.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000] = "File opened";
fopen("test.txt", "w");
fprintf("%s", sentence);
fclose();
return 0;
}
The error showing me is:
error: too few arguments to function 'fclose'
How can I do that? Please help me.
In future recommend that you do some searching for either existing questions that people have asked or some research. ie look up the manual or spec for fopen: https://man7.org/linux/man-pages/man3/fopen.3.html
Especially since your code doesn't compiled as it has errors, with the fopen, fprintf and fclose all missing arguments or assignment variables. Reading those errors and the compiler messages would have guided you to the solution. This is what it would look like with those fixed and the "w+" option used:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000] = "File opened";
FILE *file = fopen("test.txt", "w+");
fprintf( file, "%s", sentence);
fclose( file );
return 0;
}
This is a good tutorial if you want to know more: https://www.geeksforgeeks.org/basics-file-handling-c/

How to write a file, then make it executable in C?

I am trying to write a program that writes a file with some text in it, then makes that file executable. This is what I have:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char name[] = "foo";
FILE * fp;
fp = fopen(name, "w");
fprintf(fp, "This file should be executable.\n");
execl("/usr/bin/chmod", "/usr/bin/chmod", "+x", name, NULL);
return 0;
}
The problem I am having that running execl seems to remove the contents of the file I wrote. If I remove the execl, it works as expected, and writes a file with the desired text. But when I leave in the execl, it writes a file, makes it executable, but the file is blank. How do I make it so the file still has the text in it, and is executable?
The problem is that output to the file is buffered. When you call execl(), you replace the process with chmod, but never write the stdio buffer to the file.
You need to call fclose(fp) before execl() to force everything to be written. You could also use fflush(fp), but fclose() more complete.
Creating the file with the wrong permissions and then changing them is not what you want to do. Just create the file with the desired permissions in the first place. If you want the file to be executable you can (with one caveat mentioned below) run:
/* CAUTION: all error checking omitted for clarity */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int
main(void)
{
char name[] = "foo";
FILE * fp;
int fd = open(name, O_RDWR | O_CREAT, 0777);
fp = fdopen(fd, "w");
fprintf(fp, "This file should be executable.\n");
return 0;
}
This will create the file with mode 0777 (modified by the umask) when executed. (eg, if umask is 111, the executable bits will not be set)
But, if you do want to create the file with the wrong permissions and then change them, don't exec out to /usr/bin/chmod. Just use chmod(2). eg chmod(name, 0777);
+x is convenient if you are just adding a permission, but since you are creating the file you are in complete control and you know what the permissions are.

Opening a .txt file in Xcode using C and fopen( )

I can't seem to get a file to open in C. What am I doing wrong? The file is in the same directory as the .c file and I think I got all the syntax. Here is a screenshot:
The output says that the file pointer is NULL.
For that to work, the "test.txt" has to be in the same directory as the compiled binary (which xcode may not be putting in the same directory as main.c - it may be in Products? I'm not so sure with xcode). Try giving the fully-qualified pathname to test.txt in the call to fopen.
If fopenfails, fp is set to NULL and errno is set according. To see why, try:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int
main( void )
{
int status = EXIT_SUCCESS;
FILE *fp = fopen(“test.txt”, “r”);
if( fp == NULL )
{
fprintf( stderr, “Errno %d, Error %s, opening text.txt for reading.\n”, errno, strerror(errno));
status = errno;
}
// Do something with fp...
return(status);
}
To open the file from any directory, pass the file name in argv, check for arguments and use that parameter to main as the file name( pref. after copying to a dedicated variable).

input redirection on CMD

Well, I am learning programming in C, and I got an assignment to get 3 characters from an input text file into 3 variables and then print their ASCII values.
I wrote this code:
#include <stdio.h>
int main()
{
char a,b,c;
printf("Insert 3 characters:\n");
a=getch();
b=getch();
c=getch();
printf("%d, %d, %d",(int)a,(int)b,(int)c);
}
I opened a text file (input.txt) and wrote there: "abc".
I managed to compile the code with the MinGW compiler, and on the CMD window that I opened in the folder of the .exe file, I wrote: "Task.exe <input.txt".
The program ran normally. I mean, it waited for me to input 3 characters.
What have I done wrong in my work?
help me please :)
You are asked to read from an input text file.
Why don't you use fopen to open a file handle, and fgetc to read from it?
You could perhaps use fscanf. Don't forget to use the resulting count.
And of course, you should call fclose. Using perror is useful to handle error cases.
So start your code with something that checks that your program has an argument, then fopen it:
int main(int argc, char**argv) {
if (argc<2) { fprintf(stderr, "missing program argument\n");
exit(EXIT_FAILURE); };
FILE* fil = fopen(argv[1], "r");
if (!fil) { perror(argv[1]); exit(EXIT_FAILURE); };
Then run Task.exe input.txt in your console (no redirection needed!).
You should take the habit of reading the documentation of every function you are using, of testing failure cases, of compiling with all warnings & debug info (gcc -Wall -Wextra -std=c99 -g), and of using the debugger (gdb).

Linux terminal file opening

This is some code that I wrote but I am confused to how to run it with linux terminal.
I tried writing like this:
asdasd:~/folder/file>./main.c file.txt but I just keep getting permission denied.
Do I need some other program to run this with? I hope I gave enough information to get some kind of feedback
(file.txt is the file I am trying to start the program with)
...........
void fileReader(int number, char *vector[])
{
if(number!= 2)
{
printf("File: %s filename\n", vector[0]);
exit(1);
}
FILE *file = fopen(vector[1], "r");
if(file == 0)
{
printf("File cannot be opened\n");
exit(1);
}
..........
........
You have to compile the program.
You do that with
gcc main.c -o program
Then you start it with:
./program file.txt
depending on which Linux OS you're running with (MAC already has this pre installed), just go to the terminal, and change the path to the folder where your program is. then do gcc main.c -(any file name that you want, you can even just name this 'main'). then you can just type main, and it'll run your program for you.

Resources