C++ :: Function To Replace A File With The Contents Of Another Stream
Apr 26, 2012
I have function which will replace (or create) an file with the contents of another stream. The stream could be anything. The replacement is done safely.
Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
int do_replace(const char *file, int stream, int cnt) {
[Code] .....
View 14 Replies
ADVERTISEMENT
Apr 29, 2013
I wrote a program to write text contents to file stream through fputs, the file stream address was changed in the middle of writing text content to the stream (11% text content have been put into the file stream), that cause the file stream pointer can be evaluated problem and raise exception on stream validation code in fputs library function, my question is what things could go wrong to make file stream pointer changed its address to something else or a NULL pointer if the file stream have not been flushed and closed.
View 5 Replies
View Related
Feb 19, 2015
I'm learning internet sockets right now and to that end I've made a simple client/server chat program centered around select(). I've got it to where multiple messages can be sent and received on either side and the "prompt" will move down 1 line each time accordingly.
My only sticking point is when someone is in the middle of typing a message and a new message is received. The message they are currently typing is going to be deleted, so they'll have to start over again. What I want to do is grab the current contents of the stdin buffer (meaning, there's no ), save it, print the received message and move the prompt downward as usual, and then put that saved message back into the buffer, meaning not only is it back on the screen now, it's erasable too as if nothing ever happened.
I know that this will definitely be some very very non-standard C, and that's fine. To that end, I've read that curses, GNU readline, and termios are possibilities for this. I've tried those, but am having trouble making it work.
This will be a moot point when I put a GUI on it soon (probably wx, but maybe Qt) since it won't even be an issue, but I'm determined to make this work. Both systems (the "client" and the "server") are Linux, one being Ubuntu and one being Debian.
View 2 Replies
View Related
Dec 23, 2014
I am new to C, but I recently completed an exercise from a book that finds a string in another string, removes the string, and replaces it with another string.
Code:
// Replace String
#include <stdio.h>
#include <stdbool.h>
bool replaceString (char source[], char s1[], char s2[])
}
[code]....
View 8 Replies
View Related
Jan 3, 2013
I'm building a find and replace function for my text editor I'm building the function without support from the algorithm header.
The function is written like: doc.find_replace("Word_to_be_replaced", "The_word_that_is_replacing"); I find this very easy to understand replace this, with this.
find_replace will both have char * as their arguments.
The problem I'm having right now if I replace a bigger word for a smaller word how do I delete the extra characters from memory.
So if I replace "Goodbye" with "Hello" how do I delete the last two characters? So I don't print garbage code.
View 1 Replies
View Related
Feb 14, 2015
I have a question about one function in my program. Write a function that will replace players in a team. New player gets in the game, and takes the place of one that leaves. Prototype of function is:
void replace(TEAM *p,PLAYER newplayer,int num)
where second parameter is new player, and the third is a jersey number of player who leaves the game.
Two structures are defined as:
typedef struct {
char name[25],surname[25];int number;
}PLAYER;
typedef struct {
char nameofteam[25];int numberofplayers;PLAYER *players;
}TEAM;
First I tried to read which player should get out, but that didnt work:
printf("which player should get out?");
do {
scanf("%s",p->players.number)
} while(p->players.number);//Choose one of previously read players
Second thing is to read a new player and replace him with the chosen who leaves.
View 3 Replies
View Related
Sep 23, 2014
I'm supposed to add two new functions to an existing class that I've written early on: readData(ifstream&)and writeData(ofstream&).
The parameters of the program are:
-Create three employee objects as shown,
-Create an ofstream object and open a file. Choose any name for the file that you want. Do not ask the user for the file name. Pass just the file name as the parameter (no path) so that your program assumes the file to be in the same folder as your executable file.
-Send messages to each of the three Employee objects to write themselves out to the file.
-Close the file.
...
If I get writeData(ofstream&) function bit to work! for brevity I've cut all the functions of the class that arent necessary. Here's what I have so far:
Employee.h
#pragma once
#include<string>
using namespace std;
[Code] ....
So I've tried a bunch of different ways to get my objects into the ofstream object to write them to the file, but I'm supposed to use the two new functions somehow...but I'm way lost.
View 5 Replies
View Related
Mar 6, 2015
I'm having a bit of problem. I've set the string 'Absent' to all student in a file. However, I want to replace 'Absent' with 'present' when the correct ID assigned to a student is entered. In other words, 'Absent' will only change to 'Present' for a specific person at a time. I'm not sure how to implement this
View 4 Replies
View Related
Nov 4, 2014
I have a text file, like that:
(A1*
*
*
*A1)
*
(AM-MNR*AM-MNR)
(V*V)
(C-A1*
*
*
*
C-A1)
Now, I would like to replace some elements, follow the rule:
-(A1* ---> B- A1
- characters "*" following is replace by I-A1
- *A1) ---> I- A1
The output like that:
B- A1
I- A1
I- A1
I- A1
*
B-AM-MNR
(V*V)
B-C-A1
I-C-A1
I-C-A1
I-C-A1
I-C-A1
How can I do now?
My text file have 220 663 lines and 858 995 characters (I used Linux commands : wc to count them. If I use array to store data, I think it's too large array. But if use fstream, I dont know to access each element in stream to compare and replace. I dont know How many labels such as: A1, C- A1, AM- MNR... which I have to replace.
View 1 Replies
View Related
Nov 10, 2014
I adapted the code from the following: [URL]
To replace a string in my file with another. The file contents look like the following:
abc 123 0
"abc"= The username
"123"= Their password
"0"= The amount in their bank
I want to change their bank amount.
But, it isn't working, nor is it even giving me an error code.
// When the user logs out, make changes to their bank
string strNew = std::to_string(player.bank); // String representation of the revised player bank
ifstream filein("user_accounts.txt"); // Old repository of player data
ofstream fileout("user_accounts_new"); // New repository of player data
[Code].....
View 2 Replies
View Related
Jun 3, 2014
I want to remove some character in my file. too i want to replace some char to another. I know the easiest way for this job is we write final text in file. i mean we first produce sightly text then write it in file . I don't want use of temporary string.
View 3 Replies
View Related
Jun 29, 2013
I want to read a file and replace a particular line in a file.
Ex:
temp.txt file contains below
FilePath1: xxxxxxxxxxxxxxxxxxxxxxxxx
FilePath2: xxxxxxxxxxxxxxxxxxxxxxxxx
FilePath3: xxxxxxxxxxxxxxxxxxxxxxxxx
Mode: 1
Here My requirement is replace the 4th line Mode: 2 instead of Mode: 1.
I was used seek method like below,
Code:
CStdioFile SaveFile;
CString strFilePath;
strFilePath = _T("C:Sara emp.txt");
if (SaveFile.Open(strFilePath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite), &fileException) {
[Code] ....
The above code not working correctly.
View 14 Replies
View Related
Feb 3, 2013
i have a text file... with example following content:
Pizza 23232
Car 44,495
Drink 3493,90494
....
..
.
and so on..
no i want to find the 44,495 in this textfile.. calculate a new value like 44,495 x 2 + 5
and write the same file as
Pizza 23232
Car 93,99
Drink 3493,90494
....
..
.
back..
View 2 Replies
View Related
Jul 1, 2014
I have a program that gives the user the option to create an employee ID, name, address and so on. It also has the option to search employee by employee ID.
What I want to know is how to add and replace employee info. For example employee changes address. Also how to delete employee information from text file.
Here's my code:
//This program uses file input and output for an employee information system.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
ifstream inFile; //declare ifstream as inFile and ofstream as outFile
ofstream outFile;
[Code] ....
View 1 Replies
View Related
Dec 4, 2013
Why does my loop loops 3 times when i have only 2 line of data in my file? From the cout << s.size , when i run it will show 1,2,3.
Information in txt file
Code:
local john 10/2/1990
international tom 2/5/2000 output Code: local john 10/2/1990
international tom 2/5/2000
international tom 2/5/2000 Code: ifstream in(filename);
[Code] ....
View 4 Replies
View Related
Mar 4, 2013
I'm having some trouble with copying one I/O stream into another. I've put the first one into an array but I cannot get my second prompt to copy the .txt file the first prompt sees and outputs to the console. When I try and grab the info from the .txt file my first prompt sees I only see blank space in my .txt file.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
using std::ofstream;
[Code] .....
View 1 Replies
View Related
Oct 13, 2013
#include<fstream.h>
#include<conio.h>
#include<string>
class CLS {
public : string name;
[code]....
I have used the above code to write the class instanc to a file "Text.txt"...But it seems that "f.write((char*)&c, sizeof(CLS));" is not working properly with string. The data can easily be written using stream object!
#include<fstream.h>
#include<conio.h>
#include<string>
class CLS {
public : string name;
[code]....
When I tried to read the data on the file...., it gave an error "Thread stopped...violation.."
View 7 Replies
View Related
Feb 12, 2013
I'm writing a program that stores records into a file and then these records can be printed out. A last name, first name, and score is stored to be exactly 36 characters long (using leading spaces to pad) making it easier to retrieve specific records. For example, three records stored in the file would like like this: (the underscores are simply to illustrate the distance, they are not in the file itself)
_______lastname_______firstname__90__________lname __________fname_100___________last___________first __60
When printed out, the names are formatted as follows:
lastname, firstname: 90
lname, fname: 100
last, first: 60
However, when I print them out this is what I get:
lastname, firstname: 90
lname, fname: 100$
last, first: 60H
For some reason, for any record after the first, an extra character is added to the end. These characters are not in the file, so I was thinking that the array for some reason wasn't being filled completely, (the array is initialized to size 36 and 36 characters are read from the file using fread) so it was printing out a random character assigned to the 36th array position. Except the character never changes, (always a $ for record 2, H for record 3, l for record 4 if i remember) and I've tried reducing the array size or the number of character read and it's the string that gets altered, the random character always remains. I figure the problem must be in the print_records function (appending seems to work no problem). Anyway here is my print records and appending records code.
Code: /*
- Prints a single record stored in the file pointed to by ifp.
*/
void print_record(FILE *ifp, int record) {
[Code]......
View 6 Replies
View Related
Mar 8, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char buffer[256];
FILE * myfile;
myfile = fopen("read.txt","r");
[Code]...
I also got an error in printing wherein the data in read.txt is "Hello" newline "World" and my program prints world twice.
View 3 Replies
View Related
Jun 14, 2014
I am trying to create a tennis tournament simulator. There are 128 players in the tournament, and 32 seeds. I have a vector filled with 'Player' objects named 'Players'. Each 'Player' object has three member variables, Strength, Ranking, and ATPPoints. When the vector 'Players' is first declared, the default constructor for each 'Player' object is used, which initialises each of the 'Player' objects to zero. I then generate a Strength value in the range of 0-10 for each of these 'Player' objects, and sort them descendingly according to their strength.
I now want to create the draw for a tournament. I declare a vector 'Draw' with 128 'slots'. Each slot initially contains a blank 'Player' object, with its member variables set to 0. I first place the top 32 players throughout the draw such that they do not meet until later rounds as they would be in real life. For the remaining players, I position them in the draw by generating a random number between 0 and 127. Some of the slots 0-127 are occupied by seeded Players. These seeded Players have non zero Strength and Rank values, so to avoid overwriting these slots, I check the Strength value of the 'Player' object in the slot in question. A return value of 0 would indicate an empty slot.
At the beginning of the function, I output the Strength and Rank values of the 'Player' objects already in the draw. This outputs correctly.
for (Player& p : Draw)
{// outputs the draw in it's current state, with the already placed seeds as expected.
cout << p.GetRanking() << " " << p.GetStrength() << endl;
}
[Code]....
View 7 Replies
View Related
Feb 3, 2015
I have to develop a C program that reads a very complex txt file (output of a calculation code, so full of mixed text and numerical tables), search for a specific table in it and individuate/print or replace a specific value with assigned position in the table.
View 2 Replies
View Related
Nov 28, 2014
I'm making a binary file that has 100 "empty spaces", and then I want to rewrite specific place with info, however it always writes the info at the end of the file, no matter what I try to get position before I call write() it tells me correct position...
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
[Code].....
View 5 Replies
View Related
Jul 15, 2014
How to search for a word in a text file and replace it with a new one. Such as employee address, or first name, etc.
Here's my code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
ifstream inFile; //declare ifstream as inFile and ofstream as outFile
[Code] ....
It is under choice 3, most of my program runs correctly but I am just stuck at this part.
View 3 Replies
View Related
Apr 7, 2014
I am currently trying to read in a file that takes in the input in the following form.
Code:
HANK>25 BOB>31 AL>54
BILL>41 ABE>63 JEFF>50
I have tried the following solution:
Code:
#include<ifstream>
#include<iostream>
using namespace std;
struct node
{
string name;
int age;
);
int main ()
{
[code]....
The problem is a.name, it's extracting the entire string before the space character causing p.age to contain "BOB" and so on. I've tried using p.get(p.name, sizeof(p.name), '-') with '-' as the delimiter and p.getline() using a character array instead of a string. How would it be possible to only copy the string into a.name before the '>' character?
View 1 Replies
View Related
Sep 20, 2013
Let us assume I have a program that takes the name of the output file from the command line. Now let us assume that a decided not to give any output file location but wanted my program to to print my strings/ints ... directly to stdout. how would one do this using file streams? Using file pointers and c that is pretty straight forward but how would i achieve this in c++.
View 12 Replies
View Related
May 9, 2014
#include <cstdlib>
#include <stdio.h>
#include <string>
[Code]....
I'm trying to load a file input stream to load all the character content into a variable (say from a .java file for example), but for some reason whenever I type in the name of the file I want to stream I get the report: RUN FAILED (exit value 1, total time 2s)
View 7 Replies
View Related