473,503 Members | 9,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read/Write file in c++

11 New Member
Dear friends,

im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am now forced to do some programming as a small part of my thesis work. Here goes my task and question.

I want to read the text file and jus find the displacement old value and replace them with new value and write them in another file.........(In my analysis, an input file has been created contains some variables.For ex: a displacement value the the only variable for several analysis which i need to change to perform different analysis)

Jus I made a small program for it by using all my C++ knowledge (!!!!!!!)

To replace 0.000(old value) to 1.413(new value) and write it in another file .

I didn’t use getline () function as it is trying to replace the whole line at once even if there r two values to replace within a line.

I donno whether it is possible using getline function(I didn’t succeed replacing all the values in a line by using it)


Here comes my text file

Samp1.txt: contains

0.000 God save us programmers

I don’t do programming here after 0.000 for sure 0.000



Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     string a;
  9.     string string1;
  10.     string string2 = "1.413";
  11.     string string3 = "0.000";
  12.  
  13.     fstream inputFile("C:\\TEMP\\samp1.txt");
  14.     fstream outputFile;
  15.     outputFile.open("C:\\TEMP\\samp2.txt");
  16.  
  17.     while (!inputFile.eof()) {
  18.         inputFile >> a;
  19. //cout << a ;
  20.         int spot = a.find(string2);
  21.  
  22.         if (spot >= 0) {
  23.             cout << "found at" << spot << " the string" << string2 << endl;
  24.             string1 = a;
  25.             int b = string3.length();
  26.             string1.replace(spot, b, string3);
  27.             a = string1;
  28.             cout << a << endl;
  29.             cin.get();
  30.         }
  31.         outputFile << a << endl;
  32.  
  33.     }
  34.     outputFile.close();
  35. }
  36.  
Output:
Samp2.txt: written file contains output like

1.413
God
save
us
programmers
I
don’t
do
programming
here
after
1.413
for
sure
1.413
1.413 (****)


1. I want to hav my output file looks the same as my input file (only with a change of value replacement) not like this now I hav
2. while loop executes eof () true value at the end of file and prints again the value “a” (which stored last …. refer output (****))


may b sound simple and sillyfor u, but for me its complex

i want my output looks like

Samp2.txt:

1.413 God save us programmers

I don’t do programming here after 1.413 for sure 1.413


Will you give me some sugesstion to get my output in such a manner?

some new functions or methods?
May 29 '07 #1
5 18751
Savage
1,764 Recognized Expert Top Contributor
Problem is probably here:

inputFile >> a;

this extractor will input data in string a until reaching a whitespace.

Try using getline()

Savage
May 29 '07 #2
sheriff
11 New Member
Problem is probably here:

inputFile >> a;

this extractor will input data in string a until reaching a whitespace.

Try using getline()

Savage

Thx savage

as i mentioned earlier, by using getline() function i hav not succeed

can u tell me how can i replace a particular string from a line

int main()
{
string a;
string string1;
string string2="0.000";
string string3="1.413";
string line;
fstream inputFile("C:\\TEMP\\samp1.txt");
fstream outputFile;
outputFile.open("C:\\TEMP\\samp2.txt");
while(!inputFile.eof())
{
//inputFile >> a;
getline(inputFile, line);
cout << line <<endl;

cout <<"line is "<< line << endl;
cin.get();
int spot = line.find(string2);

if(spot >= 0)
{
cout<<"found at"<<spot<<" the string" <<string2<<endl;
// string1 = line;
int b=string3.length();
line.replace(spot, b, string3);
//line = string1;

}
outputFile<< line<<endl;
//cout<< line <<"is written"<<endl;
//cin.get();
}
outputFile.close();
}

For ex:
my input file contains:

God, save us 0.000 programmers 0.000

i want the output as

God, save us 1.413 programmers 1.413

but the output is
God, save us 1.413 programmers 0.000

whethe i need to use any loop within a line?
May 30 '07 #3
Savage
1,764 Recognized Expert Top Contributor
Yes,u do need to use a do while loop,like:

int spot;
Expand|Select|Wrap|Line Numbers
  1. do{
  2.  
  3.     spot = line.find(string2);
  4.  
  5.     if(spot >= 0)
  6.     {
  7.         cout<<"found at"<<spot<<" the string" <<string2<<endl;
  8.         // string1 = line;
  9.         int b=string3.length();
  10.         line.replace(spot, b, string3);
  11.         //line = string1;
  12.  
  13.     }
  14. }while(spot<=line.length());
Savage
May 30 '07 #4
sheriff
11 New Member
Yes,u do need to use a do while loop,like:

int spot;
Expand|Select|Wrap|Line Numbers
  1. do{
  2.  
  3.     spot = line.find(string2);
  4.  
  5.     if(spot >= 0)
  6.     {
  7.         cout<<"found at"<<spot<<" the string" <<string2<<endl;
  8.         // string1 = line;
  9.         int b=string3.length();
  10.         line.replace(spot, b, string3);
  11.         //line = string1;
  12.  
  13.     }
  14. }while(spot<=line.length());
Savage
thx much savage i did the same
May 30 '07 #5
Savage
1,764 Recognized Expert Top Contributor
thx much savage i did the same

Glad to help!

Savage
May 30 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

22
13211
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
1
4292
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
5
2240
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
23884
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
5072
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
3
18953
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
2
12584
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
2
5439
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
9
3833
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
1
3917
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.