Write In File In C

[Solved] Write In File In C | C - Code Explorer | yomemimo.com
Question : write in file in c

Answered by : josejuanse

#include<stdio.h>
int main(){	FILE *out=fopen("name_of_file.txt","w");	fputs("Hello File",out);	fclose(out);	return 0;
}

Source : | Last Update : Thu, 07 May 20

Question : how to read and write to fiel n c

Answered by : amir-ammar

#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
/* * Functions Signatures * */
size_t my_strlen(const char * str);
void my_strcat(char * dest, const char * src);
bool start_with( char* s, char* prefix);
unsigned int num_words(const char * str);
char * get_chapter_file_name(const char* prefix, const char* s, const char* suffix );
int main(int argc, const char * argv[]) { unsigned int total_lines = 0, total_words = 0, total_chars = 0; char *buf = NULL; size_t buff_size = 0; FILE *f_in = fopen(argv[1] , "r"); FILE *file_name = NULL; char * chapter = get_chapter_file_name("Moby-","PREFACE\n", ".txt" ); file_name = fopen(chapter, "w"); if(argc != 2) { fprintf(stderr, "\nUsage: %s <file-path>\n", argv[0]); return 1; } if ( ( !f_in ) || (!file_name) ) { printf((const char *) stderr, "Filed to open %s", f_in); exit(-1); } unsigned int n_words=0, num_chars=0, num_lines=0; while( getline( &buf, &buff_size, f_in ) != EOF) { if (start_with(buf, "CHAPTER")) { fclose(file_name); total_lines += num_lines; total_chars += num_chars; total_words += n_words; printf("%-30s : %d lines, %d words, %d characters\n", chapter, num_lines, n_words, num_chars); free(chapter); num_lines = 0; num_chars = 0; n_words = 0; chapter = get_chapter_file_name("Moby-", buf, ".txt"); file_name = fopen(chapter, "w"); if (!file_name) { exit(-1); } } fprintf(file_name,"%s", buf); num_chars += my_strlen(buf); n_words += num_words(buf); num_lines++; } printf("TOTAL : %d lines, %d words, %d characters\n", total_lines, total_words, total_chars); free(buf); fclose(f_in); fclose(file_name); return 0;
}
/* Find the length of a string */
size_t my_strlen(const char *str) { int i = 0; while (*str) { i++; str++; } return i;
}
/* I did not need this function * However I defined it that way */
void my_strcat(char *dest, const char *src) { unsigned int len_of_des = my_strlen(dest); unsigned int len_of_src = my_strlen(src); while (*dest) { ++dest; } while (*src) { *dest = *src; src++; dest++; } unsigned int go_back = (len_of_des + len_of_src); while( go_back != 0 ) go_back--, dest--;
}
/* This function will Catch * char buffer * That is coming from the mobybock.txt * Will return true if the buffer contains the prefix -> Chapter */
bool start_with(char *s, char *prefix) { char *s_ptr = NULL; char *prefix_ptr = NULL; for (s_ptr = s, prefix_ptr = prefix; *prefix_ptr; s_ptr++, prefix_ptr++) { if ( (*s_ptr) != (*prefix_ptr) ) return false; } return true;
}
/* Counts number of words inside a string */
unsigned int num_words(const char *str) { if (str == NULL) return 0; bool is_one_space = true; int words = 0; while (*str != '\0') { if ((*str == ' ') && is_one_space) { is_one_space = false; ++words; ++str; } else if ((*str != '\n') && (*str != '\t')) { is_one_space = true; ++str; } else { ++str; } } return words;
}
/* Will return each chapter as a txt file name */
char *get_chapter_file_name(const char *prefix, const char *s, const char *suffix) { unsigned int len_s = my_strlen(s); unsigned int len_prefix = my_strlen(prefix); unsigned int len_suffix = my_strlen(suffix); size_t size = len_prefix + len_s + len_suffix; char *arr = malloc( sizeof(char) * (size+1) ); *arr='\0'; unsigned int count_prefix = 0; my_strcat(arr,prefix); arr[len_prefix+1] = '\0'; /* Adding "-" for white spaces */ char * end_of_s = s; end_of_s += len_s - 1; unsigned int i= len_prefix; for (; *s && (s != end_of_s-1) ; count_prefix++, s++) { if ( (*s == ' ') ) { // Add '-' if there is a space arr[i++] = '-'; } else { arr[i++] = *s ; // Else add the char; } }arr[i]= '\0'; my_strcat(arr,suffix); arr[i + len_suffix] = '\0'; return arr;
}

Source : | Last Update : Thu, 05 May 22

Question : Read Write in File in C language

Answered by : muhammad-abdullah-jxze1et1y350

//////WRITE
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num; FILE *fptr; // use appropriate location if you are using MacOS or Linux fptr = fopen("C:\\program.txt","w"); if(fptr == NULL) { printf("Error!"); exit(1); } printf("Enter num: "); scanf("%d",&num); fprintf(fptr,"%d",num); fclose(fptr); return 0;
}
//////READ
#include <stdio.h>
#include <stdlib.h>
int main()
{ int num; FILE *fptr; if ((fptr = fopen("C:\\program.txt","r")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } fscanf(fptr,"%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0;
}

Source : | Last Update : Mon, 03 Oct 22

Question : write to file in c programming

Answered by : stupid-shrike-iejojj9s7thy

 [...] printf("Enter name: \n"); if (fgets(name, sizeof name, stdin)) { fputs(name,fileptr); fclose(fileptr); printf("File write was successful\n"); } else { printf("Read error.\n"); }

Source : https://stackoverflow.com/questions/8115218/writing-user-input-to-a-file-in-c | Last Update : Sun, 16 May 21

Answers related to write in file in c

Code Explorer Popular Question For C