C# :: StreamReader And StreamWriter - Overwriting Specific Line In A File?

Apr 10, 2014

I am currently working on a small project, which is an ATM simulator but, lately, I've been stuck in a part where I have to overwrite a specific line in a .dat file to update the account's checking and savings balance, once the transaction has been done. I have to use StreamReader and StreamWriter to complete the task and I have also being told to try Collections... which I'm not sure as to why.

Here's my code so far:

private void btnOKd_Click(object sender, EventArgs e) {
int transaction = 0;
atmTIMER.Elapsed += new ElapsedEventHandler(timerRESTART);
FileStream fileTRANSACTIONS = new FileStream("C:\Users\Voyage\Desktop\ATMSim\ATMsim\Transactions.dat", FileMode.Open);

[Code] .....

View 14 Replies


ADVERTISEMENT

C# :: StreamWriter Not Writing To A New Line

Apr 15, 2015

I have the following code to write to an error log that I send to a client. It correctly logs all of the errors but for some reason it produces it all out on 1 line and it's not removing the last comma. You can see on line 14 I am calling writeline instead of write and I'm passing a false to the formatAsQCD method that should cause it not to place the comma.

Logging snippet

errorLog.Write(formatAsQCD(rid));
errorLog.Write(formatAsQCD(M.subscriberId));
errorLog.Write(formatAsQCD(M.firstName));
errorLog.Write(formatAsQCD(M.lastName));
errorLog.Write(formatAsQCD(M.effectiveDate.ToShortDateString()));
errorLog.Write(formatAsQCD(M.correspondenceAddress.address1));
errorLog.Write(formatAsQCD(M.correspondenceAddress.address2 + (M.correspondenceAddress.address3

[Code] .....

Returned Output (personal info blanked out)
"103066139999","5000052166-00","XXXX","XXXX","2015-01-24","XXXX","","XXXX","XX","XXXXX","XXXX","20150407","Member has no provider choices that match the necessary criteria","102254726799","5000075745-00","XXXX","XXXX","2015-02-26","XXXX","","XXXX","XX","XXXXX","XXXX","20150407","Member has no provider choices that match the necessary criteria",

Expected Output (personal info blanked out)
"103066139999","5000052166-00","XXXX","XXXX","2015-01-24","XXXX","","XXXX","XX","XXXXX","XXXX","20150407","Member has no provider choices that match the necessary criteria"
"102254726799","5000075745-00","XXXX","XXXX","2015-02-26","XXXX","","XXXX","XX","XXXXX","XXXX","20150407","Member has no provider choices that match the necessary criteria"

View 10 Replies View Related

C/C++ :: Delete Specific Line In Text File Using Program?

Feb 6, 2014

Some codes to delete a text in a specific line in a text file.

Example:
This is line 1
This is line 2
this is line 3

Removing line 2,

This is line 1

This is line 3
//////////////////

View 2 Replies View Related

Visual C++ :: Program To Output A Line For A Specific Age Group - CPP File

Feb 13, 2013

I am suppose to make a program that will output a line for a specific age group,but everytime I execute the exe file it gives me the desired line and also the last line included everytime.Where did I go wrong with it including the last line everytime?

#include <iostream>
using namespace std;
int main() {
double age;
cout<<"Enter your age:";
cin>>age;
if (0<age && age <6)

[Code] ......

View 1 Replies View Related

C++ :: Reading From TXT File / Replacing Word And Then Overwriting

Nov 21, 2014

My goal is to open a .txt file and replace words in it then overwrite it. My goal is half way done. It couts it perfectly, but I am having a hard time overwriting the whole text file exactly how it couts it. I want the text to be exactly how it is displayed. In this example, I want to replace "ABC" with "DEF".

Text file "A.txt":
1 2 ABC 4 5
6 ABC 8

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
string Y;
fstream IN("A.txt", ios::in | ios::out);

[Code] ......

View 3 Replies View Related

C++ ::  Going To A Specific Line Through Ifstream

Jan 12, 2015

I want to access specific lines in a "*.txt" file. I've heard of seekp() and what not, but don't know how to call them as such.

View 6 Replies View Related

C++ :: Processing Large Files - Getting A Specific Line

Jul 5, 2012

I am reading in some large files to process. The files need to be parsed into multi-line sub units for processing (by a different physical process). My current setup is crude and parses the entire file into memory before beginning to process the sub units. This is fine as long as the file fits, so I get into trouble > 2GB or so when my machine flat runs out of memory. The simple thing to do is to only read in some of the file, process what was read in, and then read in more. I more or less know how to stop reading at some point in the file, but I'm not sure how to resume from that point later when I need more data. Is there a way to count lines and then getline() starting from a specific point in the file? From reading the getline() doc, it doesn't look like there is.

This is my current function,

Code:
void ParseFile(char *path, char type) {
// open input file
ifstream input_file(path);
if( !input_file.good() ) { cerr << "Failed to open " << path << endl; exit(ERRCODE_ERROR); }
int pos = 1;

[Code] ....

I need to remove the code to open the file from this function and open it elsewhere. I would like to call this every time I run out of data, if( tasks.empty() ), and get more data from the input file, but I don't know how to resume reading the input file from where I left off.

If this is not possible, I guess I would have to place a function call in while( input_file.good() ) to call out and process the data I have. When the current tasks list is finished, the list could be cleared and control returned into the while loop to continue reading input and start re-populating the list.

View 2 Replies View Related

Visual C++ :: Letting User Enter A Character On Specific Line And Place?

Aug 8, 2014

How can I create in my C++ program so a user can paste a text and then choose a character on specific line and place?. The above thing isn't that important. I just want to place a character on a specific line and place.

I mean something like this:

Enter a character:

You choosed " / "

On which line do want the character?

You choosed "Line 1 and 2"

Where do you want the the to appear on the line? (left or right)

You choose left.

Results:

Line 1. / hello

Line 2. / hello

View 8 Replies View Related

C# :: Reading Map Data From Streamreader

Jul 16, 2014

So what I'm trying to do is read map data from streamreader, which is very new for me, I already managed to generate maps using code inside the IDE but im very confused on how I would read it from a file. What I'm trying to say is that I understand how to create a map from the map data, but I dont know how to put that map data into text files which would make my job much easier.

My Game1:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

[Code] .....

Tl;dr basically, how would I go about using streamreader to read the map, and include the required commas and curly braces. I have already used Streamreader for other things and failed miserably.

View 4 Replies View Related

C# :: StreamWriter / View Written Data Prior To Closing?

Oct 11, 2014

I've found that with streamwriter(sw) the data isn't displayed with the written to document until you either leave scope or use sw.close I'd prefer to keep sw open as I will be writing to the file in and out, sometimes very frequently, but would also prefer the data to be viewed in real time. Is there an efficient way of going about this?

View 12 Replies View Related

Visual C++ :: Get Streamreader To Read And Calculations To Populate Textbox

Jul 27, 2013

Here what i have so far, the error in the stream

[CODE]

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String = IO.File.ReadAllText("Grades.txt")
Dim GPA As Double = 0

[Code]....

View 1 Replies View Related

C :: Reading A File Line By Line (invalid Write Of Size X)

Aug 17, 2014

I am trying to read a file line by line and then do something with the informations, so my method looks like this:

Code:
void open_file(char *link) {
FILE *file = fopen(link, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file.
");
exit(EXIT_FAILURE);

[Code] ....

1) The first complain of valgrind is at the line where I use fgets and its telling me (invalid write of size x), but I have allocated my line to 56000 and the read line is shorter, why is there a write size error then :S?

2) at the line where I realloc where I try to shrink the space he's telling me: Address .... is 0 bytes inside a block of size 56000, But I know i need only this space so why is there a write over space error :S??

View 9 Replies View Related

C :: Reading A File Line By Line And Storing It Backwards Into A List

Sep 25, 2013

So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
1
2
3
4

The code should store each line in a list as such...
4, 3, 2 ,1

Instead the code will store the last variable in all nodes. So the final list will look like this...
4, 4, 4, 4

Here is my code...

struct node *head = NULL;
int i;
while(read(in, &i, sizeof(int)) != 0) {
struct node *temp = malloc(sizeof(*temp));
temp->line = &i;
temp->next = head;
head = temp;
}

View 4 Replies View Related

C :: Copying File Line By Line Using Dynamic Memory Allocation

Jul 15, 2013

I need to read lines from one file and copy them line by line into another file using dynamic memory allocation. It compiles but gives me a seg fault. Why/How?

Code:
int main(){
FILE *fp1;
FILE *fp2;
FILE *i;
fp1=fopen("file1","r");
fp2=fopen("file3","w+");

[Code] ....

View 2 Replies View Related

C++ :: Read Line By Line From Text File To String

Jul 5, 2013

I have a text file (test.txt) with the following data:

01,05,25,20130728
01,06,25,20130728
01,07,25,20130728
01,08,25,20130728
01,05,25,20130728
01,05,25,20130728
01,05,45,20130728
01,05,65,20130728
01,05,85,20130728
01,05,35,20130728
01,05,25,20130728
01,05,35,20130728

I want to read this to one string called line. So far I have this code:

string line;
ifstream myfile ("/home/Test.txt");
if (myfile.is_open()) {
while (myfile.good()) {
getline (myfile, line);
for (int a = 0; a <= 335; a++) {
cout <<line.at(a);
} }
myfile.close();
}

so far its only printing the first line and then throwing an instance of 'std::out_of_range'

View 2 Replies View Related

C++ :: Read Txt File Line By Line Write To Vector

Oct 5, 2013

I need to read a text file which has various lines containing integers. I need to write those integers separately in a vector. Example, the first line of the text file contains 3 9 8 7 6 so vector[4]=3, vector[3]=9, vector[2]=8 and so on. Next read the second line 4 1 2 3 4 5 and write to another vector vector[5]=4, vector[4]=1...

I tried the code below but it will write from the second line, the whole line in one vector index.

int str; // Temp string to
cout << "Read from a file!" << endl;
ifstream fin("functions.txt"); // Open it up!
string line;
// read line count from file; assuming it's the first line
getline( fin, line );

[Code]...

View 1 Replies View Related

C/C++ :: Reading From A File Line By Line With No Specified Number Of Values

Apr 12, 2015

Im trying to read from a file, line by line, with no specified number of values in the file. Check out my code:

int main() {
string x;
ifstream fin;
int count = 0;
char ch;
fin.open("CWC_Master.txt");
if(!fin)

[Code] .....

Now, this works great! However, its skipping some lines. And I dont know why. For example: Lets say that the input file is:

superman toy
sm2335
19.99
batman toy
bm5532
25.99
aquaman toy
am6786
26.00

Where it should output the above, instead it outputs every other one. Like:

superman toy
19.99
batman toy
25.99
aquaman toy
26.00

How can I fix my code so that it SIMPLY(i say simply because I am still a beginner coder) can read line by line?

View 7 Replies View Related

C/C++ :: How To Get Last Line 10 Char In Text File If Last Line Is Null

Aug 24, 2013

I can able to get the last line ten chars but i cant get if the last line is null.

here is my code

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {

[Code] ....

here with i have enclosed my file where i am pick my last ten chars ...

Attached Files : log.txt (4.8 KB)

View 1 Replies View Related

C++ :: String Operation While Reading Line By Line From A File

May 12, 2014

I have to read the information about logic gates from a file. The file format is shown below:

Code:
gateOne = NAND(inpA, inpB)
gate2=NAND(1,2)
3 = NAND(23,25,26)

As, it can be seen from the above structure that whitespaces are not same everytime. So, to deal with this situation, i am using boost library to remove all whitespaces from the line which is being read and then try to find the name of gate and its input. My code is given below which is able to correctly find the gate names and its first input...but my code is not able to find the second, third and so on input names.

Code:
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <boost/algorithm/string/erase.hpp>

[Code] ....

View 3 Replies View Related

C++ :: How To Make Specific Character Show Up Specific Amount Of Times

Mar 5, 2013

How do I make a specific character show up a specific amount of times?

Like I am generating a random number then I need to make "|" show up that many times on the screen.

View 1 Replies View Related

C :: Reading File Line By Line On Windows?

Oct 13, 2014

How to read a file line by line and then later access them by doing something like

Code:
lines[0] //Line number one
...
lines[100] //Line number one hundred and one
lines[100][0] //L
lines[100][1] //i
lines[100][2] //n
lines[100][3] //e
lines[100][4] //
lines[100][5] //n
...

View 13 Replies View Related

C++ :: How To Read Data From A File Line By Line

Jul 2, 2013

I have an external file with one column of data. If I have a counter value let say counter =1, and counter++ and so on. How I can write such a c++ code that if the value of counter and value from the external file are same then generate an action let say cout both values i.e. value of counter and value from external file.

for more information, here is an example:

data in file(in one column): 2 6 8 9 10...
value of counter : 1 2 3 4 5 6 7 8 9...

then cout values only if value of counter and value from the file is same.

Here is my code so far, but it does not seem to work;

#include<iostream>
#include<fstream>
using namespace std;
int main() {
const int SIZE = 10; //Size declaration of array
int hours[SIZE]; //Array declaration

[Code]...

View 12 Replies View Related

C++ :: Read File Content Line By Line

Jan 23, 2015

I am reading my file (20GB) line by line using boost like this

PHP Code:

boost::interprocess::file_mapping* fm = new boost::interprocess::file_mapping("E:Mountain.7z", boost::interprocess::read_only);
boost::interprocess::mapped_region* mr = new boost::interprocess::mapped_region(*fm, boost::interprocess::read_only);

 char* bytes = static_cast<char*>(mr->get_address()); 

An exception is thrown in the second line while allocating memory for mr.

I use boost because it can also work in Mac which my code will be ported to.

View 3 Replies View Related

C/C++ :: Bus Error 10 - Overwriting String Literals

Mar 28, 2014

So I am trying to take a string and replace a certain character with another in the string. The function doesn't return anything, but rather changes the string itself. Why would I be getting a bus error 10?

void replaceChar(char *src, char oldChar, char newChar){
//get length of source string.
int length = 0;
length = (int) strlen(src);
char *newSrc = (char *)malloc((length + 1) * sizeof(char));

[Code] ....

I feel like maybe I can't overwrite existing characters in a string, but I don't know how else to implement this without returning anything, which I am not allowed to do.

View 1 Replies View Related

C++ :: Creating Poem - Cout Overwriting Data

Mar 20, 2012

So in a nutshell I have to write a program that grabs a random line from a file and exchange the numbers for words from other files to create a poem. The problem I have is that when I go to output the parts of the ntca it gets all jumbled and writes over each other. (See output below)

I saw something about a memory leak, but I couldn't see how that could be happening in this program. It's a basic intro level program for a class and I'm just a little stuck.

Code:

void create_poem(const int poem_lines) // called from main function {
//Peform each for each line
for(int x = 1; x <= poem_lines; x++) {
char begin_line[1000];

[Code] ....

Everything works perfectly except for the output which looks like this :

Code:
A 2 2 1 and 1 3 1 // <-- this is the line before translation
junkcarrnet

As stated, I know everything else works through debugging purposes. Just the overwriting is my only problem.

View 1 Replies View Related

C++ :: TXT File - How To Get Each Specific Value And Add Them All Together

Oct 10, 2013

so I wrote this code in class today and I'm almost done but I need to come up with some sort of equation to add up all the total values. The problem is I am reading data off a txt file so it's all being read as one thing. I don't know how to get each specific value and add them all together. This is my code so far.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

[Code] .....

here's what's inside the input file

Chisel 50 9.99
Hammer 30 15.99
Nails 2000 0.99
Bolts 200 2.99
Nuts 300 1.99
Soap 55 1.89

I end up multiplying the amount by the price to get the total value as shown in the code above. Now I just don't know how to add the total value of each item to get the total overall value. Also I haven't used invTotal because I want that to be the overall total.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved