fputs

Do we need mutex to perform multithreading file IO

时光怂恿深爱的人放手 提交于 2020-01-06 07:27:12
问题 I'm trying to do random write (Benchmark test) to a file using multiple threads (pthread). Looks like if I comment out mutex lock the created file size is less than actual as if Some writes are getting lost (always in some multiple of chunk size). But if I keep the mutex it's always exact size. Is my code have a problem in other place and mutex is not really required (as suggested by @evan ) or mutex is necessary here void *DiskWorker(void *threadarg) { FILE *theFile = fopen(fileToWrite, "a+"

fputs/puts dangerous (in C)?

心已入冬 提交于 2019-12-31 07:00:24
问题 I've been having trouble with fputs lately: when printing some strings in a text file with fputs, it happens I get other characters than A-Z, a-z, 0-9 in (chars that aren't part of the string). I made absolutely sure the strings all end with the null character. Unfortunately I can't give you more information, since I did not personally test the program, that was the feedback I received. But after I replaced fputs with fprintf it worked properly. So my question is: is fputs sort of dangerous?

Last line repeats when doing fgets/fputs

只谈情不闲聊 提交于 2019-12-24 21:17:41
问题 I'm doing an insertion, that imply a file , a string , and a new file who will receive all the data of the original file plus the string to be inserted , and it will replace the original file. So, for example, my original file: data.txt line1 line2 line3 line4 line5 will become, with the insertion of the string "newline": data_temp.txt --> renamed later data.txt line1 line2 line3 line4 line5 newline With that purpose, I have the following code: /* FILE variables of the original file and the

How to read multiple lines of input from user using fgets and write it into a file using fputs in C?

試著忘記壹切 提交于 2019-12-24 12:21:56
问题 I wanted to read input from user (multiple lines) and write it into a file using fputs(). Here is my code #include<stdio.h> #include<stdlib.h> int main() { FILE *fp; char s[25]; fp=fopen("myname","w"); if(fp==NULL) { perror("Error opening file\n"); exit(1); } while(fgets(s,25,stdin)!=NULL) fputs(s,fp); fclose(fp); return 0; } After getting input from user, i am using Ctrl+C to close the input prompt of the program (I'm using linux). Then if i open the file, it contains nothing. How could I

c : gets() and fputs() are dangerous functions?

南笙酒味 提交于 2019-12-22 00:25:07
问题 In the computer lab at school we wrote a program using fputs and the compiler returned an error gets is a dangerous function to use and a similar error for fputs but at home when i type in this bit of code: #include <stdio.h> main() { FILE *fp; char name[20]; fp = fopen("name.txt","w"); gets(name); fputs(name,fp); fclose(fp); } i get no errors what so ever. The one at school was similar to this one, just a bit lengthy and having more variables. I use codeblocks at home and the default gcc

C++ Make a file of a specific size

心已入冬 提交于 2019-12-18 16:46:40
问题 Here is my current problem: I am trying to create a file of x MB in C++. The user will enter in the file name then enter in a number between 5 and 10 for the size of the file they want created. Later on in this project i'm gonna do other things with it but I'm stuck on the first step of creating the darn thing. My problem code (so far): char empty[1024]; for(int i = 0; i < 1024; i++) { empty[i] = 0; } fileSystem = fopen(argv[1], "w+"); for(int i = 0; i < 1024*fileSize; i++){ int temp = fputs

writing and reading within a fork - C

血红的双手。 提交于 2019-12-13 07:58:02
问题 The exercise is simple. The father process lets me write my name and surname in a file. The son process waits 5 seconds, then reads and displays it. I can't use the #wait() function. #include <cstdlib> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char** argv) { char name[20]; char surname[20]; char outp[50]; // The file pointer FILE *file; // Open the file in write mode file = fopen("dati.txt", "w+"); pid_t pid; pid=fork(); if(pid>0){

why when we write \n in the file it converts into \r\n combination?

二次信任 提交于 2019-12-12 17:25:20
问题 I read this concept from book that when we attemp to write \n to the file using fputs(), fputs() converts the \n to \r\n combination and then if we read the same line back using fgets () the reverse conversion happens means \r\n back convert to \n. I don't get that what is the purpose behind this? 回答1: It is because Windows (and MS-DOS) text files are supposed to have lines ending in \r\n, and portable C programs are supposed to simply use \n because C was originally defined on Unix. And it's

fputs vs. fprintf and double

好久不见. 提交于 2019-12-11 05:33:03
问题 I need to put out a double value into an file. So for all my other string i have to put out i use fputs because i think and timed it that fputs is faster then fprintf. But if i want to put out the double value i can't do this with fputs so i tried fprintf. This is very slow. I need more time if i replace the fprintf and puts there an fputs("TEST",file); How can i put out the double as fast as the string. Need i to convert it first to string? I found some conversion. But if i use them i am as

why fputs and fprintf reverse stream order

霸气de小男生 提交于 2019-12-08 17:13:59
问题 I don't get it why fputs and fprintf reverse stream order. int fputs (const char * str, FILE * stream); int fprintf (FILE * stream, const char * format, ...); ssize_t write(int fd, const void *buf, size_t count); I known fprintf put stream in forward to support variable arguments,But why fputs series don't keep consistency ??? 回答1: Because these things were written many decades ago, it's generally only be a question of interest for historians :-) It was probably just a design decision (or