The text file cannot be found [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The error says no such file or directory i moved the file to the project folder
int main(int argc, const char * argv[]) {
int i;
char singleline[150];
FILE *file;
file = fopen("test.txt", "r");
puts(singleline);
if( file == NULL ) {
perror("Error: ");}
fclose (file);
}
return 0;
}

I guess you really need help debugging why this is happening to you.
Try adding some more code to your routine to help you determine what is going on. One thing to try is to call getcwd.
#include <unistd.h>
...
char buf[PATH_MAX];
printf("cwd: %s\n", getcwd(buf, sizeof(buf)));
...
This should report to you where your program thinks it is running from.
You report you get the following output:
cwd: /Users/ahmedhossam/Library/Developer/Xcode/DerivedData/assem‌​bler-doinswpyuiekhhe‌​mczblkainroaw/Build/‌​Products/Debug `ֿ_\377
Start with that first, and I am guessing the next steps will become obvious to you.
The reported current working directory (hence, getcwd) is not your project folder. You can copy your file to that strange directory, or you can use chdir to change your working directory to be your project folder, or you can specify the absolute path to your file as suggested below.
You can avoid the problem altogether by specifying the absolute path to your file.
file = fopen("/the/absolute/path/to/test.txt", "r");

Related

Can a file move itself in C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I'm trying to make a file install itself into the system (linux).
Every method I use (rename, system(mv), execl, etc) fails. Is there anyway to make a running executable move itself while running? The closest I've come is renaming it but only within the same directory.
The code below "copies" itself to a new destination and gets deleted after termination. Basically, the code makes a new entry (hardlink) at the specified location and removes the current one (from the calling directory). The contents of the file will be preserved, since there is no copy and deletion involved.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
int main(int argc, const char *argv[])
{
//current filepath (dir + name)
const char* file_path = argv[0];
//extract filename component (needed to append to newdir)
char* file_name_ = strdup(file_path);
char* file_name = basename(file_name_);
//make new path name
char* new_dir = "<put your destination here - with no slash ('/') at the end>";
size_t new_path_len = strlen(new_dir) + strlen(file_name) + 2; //two extra bytes: '/' + '\0'
char new_path[new_path_len];
snprintf(new_path, new_path_len, "%s/%s", new_dir, file_name);
//make a new name for a file (hardlink)
link(file_path, new_path);
//delete a name and possibly the file it refers to
unlink(file_path);
//cleanup
free(file_name_);
return 0;
}
Hint: careful study of the documenation to link is highly recommended (especially the ERRORS section).
References:
basename - parse pathname components
link - make a new name for a file
unlink - delete a name and possibly the file it refers to

Where does the function fopen() in C creates the file, opened in write mode? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Where is the directory?
i've searched for possibly all the directories but couldn't find any file related to the program
In the current working directory. If it does not maybe the call is not successfully. Check its return-code.
fopen() function is used to open a file to perform operations such as reading, writing etc. In a C program, we declare a file pointer and use fopen() as below. fopen() function creates a new file if the mentioned file name does not exist. The directory where you run the program from
The following example shows the usage of fopen() function.
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * fp;
fp = fopen ("file.txt", "w+");
fprintf(fp, "%s %s %s %d", "We", "are", "in", 2012);
fclose(fp);
return(0);
}
Let us compile and run the above program that will create a file file.txt with the following content −
We are in 2012
Now let us see the content of the above file using the following program −
#include <stdio.h>
int main () {
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
Let us compile and run the above program to produce the following result −
We are in 2012

How to read file in C programming using codeanywhere? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
here is the code given to me and a have also a text file where i need to get the Text from and compile the program.
You need to use open a file with fopen() first. But the current user needs to have perms to read/write the file.
We will use r to only read from a file. If the file is not read it will return NULL. You can fscanf() function to get the value of a file. The second parameter represents the type of the variable as in this case it's a string(char), third param is the mem address of the variable itself. Kind of like file version of scanf().
int main()
{
char a[1000];
FILE *myFile;
if ((myFile = fopen("C:\\myUSER\\newprogram.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(myFile ,"%s", &a);
printf("Value of a=%s", a);
fclose(myFile);
return 0;
}

A simple text file wouldn't be created [C, GCC] [closed]

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 6 years ago.
Improve this question
Below is a simple code that I'm hoping SIZE.txt to be created, then be closed. It compiles without error, nor warning, but file wouldn't be created. However, a segmentation fault is thrown when code is executed.
Thanks for your kind help in advance.
#include "stdio.h"
void main() {
FILE *fp;
fp = fopen("SIZE.txt", "r+");
fclose(fp);
return;
}
The primary reason for the failure was due to the specification of the file mode as "r+" which will only open existing files, not create new files. A file is only created if "w+", "a", or "a+" is specified as the file mode.
check this, if file exists it will be display file already exist, but if the file doesn't exist it will be create it, depend what are you going to do, the ab+ you can just change to w
FILE *fp;
fp = fopen("SIZE.txt", "r");
if(fp==NULL){
fp=fopen("SIZE.txt", "ab+");
printf("File was created\n");
}
else{
printf("File already exists\n");
}
fclose(fp);
return 0;
Change:
#include "stdio.h"
To
#include <stdio.h>
Also, check for a NULL pointer being returned by fopen():
if ((fp = fopen("file.txt", "r")) == NULL) {
// Handle error...
}

Junk values from an input file in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have this bit of C code below. When I place printf statements to test the text from the input file, I see that I'm getting a bunch of junk values, to be more specific they are not even alphabetic or numerical, I think they are diamonds with question marks in them. I assume this means it is not processing these values the way it should be. The input file a bit of MIPS assembly code, but in this context it is only a text file. I have commented out all other parts of my program and am left with this small piece and yet I still receive the bad values. What could I possibly be doing wrong here?
The command I use to run the program on the console is:
./assembler -symbols adder.asm
Where ./assembler is the driver (argv[0])
-symbols is a tag used (argv[1])
adder.asm is the input file (argv[2])
So once opened I should be able to grab text out of this file, and it's not a problem with the file as far as I believe, it was working earlier.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
FILE *fp;
FILE *fp_out;
void main(int argc, char* argv[])
{
int mode;
if (strcmp(argv[1], "-symbols") == 0)
{
fp = fopen(argv[2], "r");
mode = 1;
}
else
{
fp = fopen(argv[1], "r");
fp_out = fopen(argv[2], "w");
mode = 2;
}
}
Try to add the following line right after the open section and add #include <errno.h> to the beginning.
printf("%p, %p, %d\n", fp, ftp_out, errno);
If the fp is null then there is some problem opening the file. If you do not check the return value, you can read from a wrong buffer. Maybe there is some permission problems (or whatever). Also if errno != 0 you have a problem. Check with perror <num> the errno value in command line (or see perror(3) function).

Resources