How to store string to txt file - c

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.

Related

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);
}
}

C opening file from console

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;
}

Write to .txt file?

How can I write a little piece of text into a .txt file?
I've been Googling for over 3-4 hours, but can't find out how to do it.
fwrite(); has so many arguments, and I don't know how to use it.
What's the easiest function to use when you only want to write a name and a few numbers to a .txt file?
char name;
int number;
FILE *f;
f = fopen("contacts.pcl", "a");
printf("\nNew contact name: ");
scanf("%s", &name);
printf("New contact number: ");
scanf("%i", &number);
fprintf(f, "%c\n[ %d ]\n\n", name, number);
fclose(f);
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* print some text */
const char *text = "Write this to the file";
fprintf(f, "Some text: %s\n", text);
/* print integers and floats */
int i = 1;
float pi= 3.1415927;
fprintf(f, "Integer: %d, float: %f\n", i, pi);
/* printing single characters */
char c = 'A';
fprintf(f, "A character: %c\n", c);
fclose(f);
FILE *fp;
char* str = "string";
int x = 10;
fp=fopen("test.txt", "w");
if(fp == NULL)
exit(-1);
fprintf(fp, "This is a string which is written to a file\n");
fprintf(fp, "The string has %d words and keyword %s\n", x, str);
fclose(fp);
Well, you need to first get a good book on C and understand the language.
FILE *fp;
fp = fopen("c:\\test.txt", "wb");
if(fp == null)
return;
char x[10]="ABCDEFGHIJ";
fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
fclose(fp);

Resources