file-handling

How do I create a file with a user-supplied name in C++? [closed]

眉间皱痕 提交于 2019-12-02 13:26:34
I want the user to be able to type some text and then the computer creates a file called what ever the text was. In C++. John Kugelman #include <cstdlib> int main() { return system("read FILE && touch \"$FILE\""); } Note: Requires POSIX. Should be OK, practically all modern OSes are POSIX-compatible. Because this looks like homework, I'm not giving a complete example. Look in: #include <iostream> (Preferred) or #include <cstdio> (Legacy) 来源: https://stackoverflow.com/questions/3427052/how-do-i-create-a-file-with-a-user-supplied-name-in-c

File Handling in C - Removing specific words from a list in text file

99封情书 提交于 2019-12-02 12:45:50
问题 I am populating a short dictionary from my basic C program using the following code : void main () { FILE *fp; fp = fopen("c:\\CTEMP\\Dictionary2.txt", "w+"); fprintf(fp, Word to Dictionary"); However I would also wish to remove certain words which I do not longer wish to be in the dictionary. I did some research and I know that " You can't remove content from a file and have the remaining content shifted down. You can only append, truncate or overwrite. Your best option is to read the file

C++ Program crashes when reading object from random access file

旧巷老猫 提交于 2019-12-02 10:03:06
I have the following User.h that holds several attributes (strings). User.cpp has all the definitions. //User.h #ifndef USER_H #define USER_H #include<iostream> #include <cstring> using namespace std; class User{ string username; public: User(); string getUsername() const; void setUsername(string); }; #endif I'm using a another class, "File" to insert new users/view users from a randomly accessed .dat file //File.h #ifndef FILE_H #define FILE_H #include "User.h" class File{ public: void loadUser(); bool addUser(User&); }; #endif File class definitions //File.cpp #include<cstring> #include

Comparing files not working as intended

笑着哭i 提交于 2019-12-02 09:31:54
hi guys could someone explain to me why this does not work. I basically have to text files called Books and NewBooks... The text files are populated from a web request and the info is then parsed into the text files...when I start the program Books and new books are identical and pretty much a copy of each other. more web requests are done to update the NewBooks text file and when I compare them if there is a line in NewBooks that is not in Books it adds that line to a third text file called myNewBooks. Now my initial code that I will show here works as I expected Dim InitialBooks = File

Garbage Value in File after write

帅比萌擦擦* 提交于 2019-12-02 08:39:23
I am making a log function which stores in a file. int log_func(char* msg) { int log_fd=0; ssize_t write_ret=0; int close_logfile_ret=0; time_t result; char error_msg[MAX_LOG_MSG_LEN]={' '}; log_fd = open("/home/pi/log_client.txt", O_CREAT|O_APPEND|O_WRONLY); if(log_fd==-1) { printf("log failed !!----file open"); exit(1); } result = time(NULL); snprintf(error_msg,MAX_LOG_MSG_LEN,"DALI_Server:%s:%s",asctime(localtime(&result)),msg); write_ret = write(log_fd,error_msg,sizeof(error_msg)); if(write_ret == -1) { printf("log failed !!---- write"); exit(1); } close_logfile_ret = close(log_fd); if

Remove uni-grams from a list of bi-grams

こ雲淡風輕ζ 提交于 2019-12-02 05:06:11
问题 I have managed to create 2 lists from text documents. The first is my bi-gram list: keywords = ['nike shoes','nike clothing', 'nike black', 'nike white'] and a list of stop words: stops = ['clothing','black','white'] I want to remove the Stops from my Keywords list. Using the above example, the output I am after should look like this: new_keywords = ['nike shoes','nike', 'nike', 'nike'] --> eventually I'd like to remove those dupes. This is what I've done so far: keywords = open("keywords.txt

Remove uni-grams from a list of bi-grams

别来无恙 提交于 2019-12-02 01:51:48
I have managed to create 2 lists from text documents. The first is my bi-gram list: keywords = ['nike shoes','nike clothing', 'nike black', 'nike white'] and a list of stop words: stops = ['clothing','black','white'] I want to remove the Stops from my Keywords list. Using the above example, the output I am after should look like this: new_keywords = ['nike shoes','nike', 'nike', 'nike'] --> eventually I'd like to remove those dupes. This is what I've done so far: keywords = open("keywords.txt", "r") new_keywords = keywords.read().split(",") stops = open("stops.txt","r") new_stops = stops.read(

need of fseek() in c

ⅰ亾dé卋堺 提交于 2019-12-01 12:05:37
Part of Code 1:- while(1) { ch=fgetc(pt); if(c==EOF) { break; } if(c==' ') { fputc('z',pt); } } Part of Code 2:- while(1) { ch=fgetc(pt); if(c==EOF) { break; } if(c==' ') { fseek(pt,0,SEEK_CUR); fputc('z',pt); fseek(pt,0,SEEK_CUR); } } I want to replace next character after every space in a file. That file is pointed by the pointer pt . Both the code shows no error and runs fine, but when I externally opens the .txt file, first code did nothing whereas the second code replaces the next character after space successfully. Clearly fseek(pt,0,SEEK_CUR); is making the difference. So I am unable to

Read file from subfolder of application installation folder

孤街浪徒 提交于 2019-12-01 11:43:19
I have to read the text content from an .txt file, this file is located in app installed folder, in a subfolder, according to Microsoft docs , I am doing it like this: private async void readMyFile() { // Get the app's installation folder. StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; // Get a file from a subfolder of the current folder by providing a relative path. string txtFileName = @"\myfolder\myfile.txt"; try { //here my file exists and I get file path StorageFile txtfile = await appFolder.GetFileAsync(txtFileName); Debug.WriteLine("ok file found:

need of fseek() in c

半腔热情 提交于 2019-12-01 10:28:24
问题 Part of Code 1:- while(1) { ch=fgetc(pt); if(c==EOF) { break; } if(c==' ') { fputc('z',pt); } } Part of Code 2:- while(1) { ch=fgetc(pt); if(c==EOF) { break; } if(c==' ') { fseek(pt,0,SEEK_CUR); fputc('z',pt); fseek(pt,0,SEEK_CUR); } } I want to replace next character after every space in a file. That file is pointed by the pointer pt . Both the code shows no error and runs fine, but when I externally opens the .txt file, first code did nothing whereas the second code replaces the next