C opening file from console - c

I have a simple code that opens a file from hdd in read mode, but the path of the file is already given in the code.I want the user to manually provide the file name,path and file open type through console. What do I have to do for that?
#include<stdio.h>
int main() {
FILE *fp;
fp = fopen("D:\\samplefile.txt","r");
if(fp != NULL) {
printf("File has been opened");
fclose(fp);
}
else printf("File not found");
return 0;
}

// Use of function scanf is done here as an simple use. There are other ways also to get input from user
#include<stdio.h>
int main()
{
FILE *fp;
char filePath[100];
printf("Enter Path name: ");
scanf("%s", filePath);
printf("value is %s",filePath);
fp = fopen(filePath,"r");
if(fp != NULL) {
printf("File has been opened");
fclose(fp);
}
else printf("File not found");
return 0;
}

#include <stdio.h>
int main()
{
FILE *fp;
char filePath[100],filePermission[10];
printf("Enter Path name: ");
scanf("%s", filePath);
printf("Enter file permission parameters: ");
scanf("%s", filePermission);
fp = fopen(filePath,filePermission);
if(fp != NULL) {
printf("File has been opened");
fclose(fp);
}
else printf("File not found");
return 0;
}

Related

How to store string to txt file

void inserting()
{
char file_name[50];
char sentence[1000];
FILE *fptr;
printf("File name (With extn):");
scanf("%s", file_name);
fptr = fopen(file_name, "a");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
scanf("%s", sentence);
fgets(sentence,sizeof(sentence),stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
}
I want to store content from string to file... but it is displaying everything except the first word..
INPUT : Hello this is C program //which I have entered
OUTPUT: this is C program //this is what stored in file
#include<stdio.h>
void inserting()
{
char file_name[50]="C:\\Users\\Dev Parzival\\Desktop\\foo.txt";
char sentence[1000];
FILE *fptr;
//printf("File name (With extn):");
//scanf("%s", file_name);
fptr = fopen(file_name, "w");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
scanf("%[^\n]", sentence);
printf(sentence);
//fgets(sentence,sizeof(sentence),stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
}
int main(){
inserting();
}
scan will not read spaces with%s u have to include spaces also I assume thats why use %[^\n]format specifier.
If you want to preserve your code make the adjustment
void inserting(){
char file_name[50];
char sentence[1000];
FILE *fptr;
printf("File name (With extn):");
scanf("%s", file_name);
fptr = fopen(file_name, "a");
if (fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
scanf("%s", sentence);
fprintf(fptr,"%s",sentence); //<-- HERE
fgets(sentence,sizeof(sentence),stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
}
Or you can use something like getline just to be cleaner.

How to keep the data in file.txt?

I want to store the data in file.txt even if I run the code again. Whenever I run the code it removes all the previous data.
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
FILE *fptr = fopen("file.txt", "w");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}
Open the file in "append" mode:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
FILE *fptr = fopen("file.txt", "a"); /* <===== changed from "w" to "a" */
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}
New data will now be added at the end of the file.

trying to save data that is in an array to a file

This is my attempt at saving to a file. Once I enter the filename, it will save a file with that name, but it does not print anything to it and I dont get why.
if (menuoption == 6)
{
printf("please enter a file name\n");
scanf("%s", filename);
filepointer = fopen(filename, "a");
if (filepointer == NULL);
{
printf("unable to open file name: %s\n", filename);
continue;
}
fprintf(filepointer, "decimal values:");
for (int i = 0; i < doubcount; i++)
{
fprintf(filepointer, "%lf \n", doubles[i]);
}
fprintf(filepointer, "\n");
fprintf(filepointer, "integer values: ");
for (int i = 0; i < intcount; i++)
{
fprintf(filepointer, "%d\n", ints[i]);
}
fprintf(filepointer, "\n");
}
Here a sample I just wrote to show you how you should handle files in C
#include <stdio.h>
int main(void){
char* filename;
FILE* fp;
printf("Enter a file name:\n");
scanf("%s", filename);
fp = fopen(filename, "w");
if(fp == NULL){
fprintf(stderr, "unable to open file name: %s\n", filename);
return -1;
}
fprintf(fp, "Testing\n");
fclose(fp);
}
First of all, you should use "w" instead of "a" in fopen function
fp = fopen(filename, "w");
You can read about the modes you can open a file here http://man7.org/linux/man-pages/man3/fopen.3.html
Then you should always close a file after reading or writing it, so add this line to your code
fclose(fp);
I hope this can work for your, unfortunately I couldn't run your code, because it lacks of variable declarations, includes and all other stuff

Why is my file pointer always NULL, even with text in it?

I am having trouble opening and parsing a file in C. As of now I am trying to open a file and print out the current line then the next until the program reaches the end of the file. However, the file pointer always returns NULL and does not read the file.
This is my function:
int file_parse() {
char filename[100];
printf("Enter the filename: ");
scanf("%s", filename); //Get filename
printf("\nYou want to encrypt or decrypt with file: %s", filename); //Confirm filename
int origin[27];
int final[27];
FILE *fp;
if ((fp = fopen(filename, "r")) != NULL) {
char current_line[250];
while (!feof(fp)) {
fgets(current_line, 250, fp);
printf("The current line is: %s", current_line);
}
fclose(fp);
} else {
printf("\nError! Cannot open the specified file.");
exit(EXIT_FAILURE);
}
}

Can't pass a char array (file name) to fopen(file,r)

I want users to enter their own file name they want into the program, then the program opens it. I don't know why it doesn't work, please help. It just works if I define the path directly on the code.
This works.
FILE *file;
file = fopen("C:\\Users\\Test\\text.txt", "r");
if (file) {
index = 0;
while ((c = getc(file)) != EOF) {
printf("ok");
}
fclose(file);
} else {
printf("Can't open file");
}
This doesn't work.
char inputFile[100];
printf("Enter file name: ");
scanf("%s", inputFile);
FILE *file;
file = fopen(inputFile, "r");
if (file) {
index = 0;
while ((c = getc(file)) != EOF) {
printf("ok");
}
fclose(file);
} else {
printf("Can't open file");
}
When reading in from code, you need to escape your "\", but scanf isn't as smart. You just need to enter the text as is!
C:\Users\Test\text.txt
Also, note that scanf ("%s", inputFile) won't handle spaces in the file name path, so "My Documents" won't work.

Resources