C++ :: Reading / Writing File On Multiple Threads?

Aug 7, 2013

I'm currently working on a server for handling clients in a 2d online game and I wrote some regular fstream file code for handling the file that stores their information and I was about to implement it into the server, then I realized there might be a problem having it open multiple times concurrently, so I googled it and came up with
posts like

[URL]

I'm wondering if I can just treat it like everything else or will I have to do something specific for opening on multiple threads?

p.s. I did read those posts but I'm very new to multithreading

View 16 Replies


ADVERTISEMENT

C++ :: Accessing And Working With Vector From Multiple Threads

Oct 20, 2014

I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.

The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.

However, if I need to switch from vectors to something else then I am open to that as well.

The following example below compiles but crashes rather quickly because it is not thread safe.

How I can fix the example below which I can then apply to my larger project?

#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {

[Code] ....

View 1 Replies View Related

C++ :: Reading And Writing Bit To A File

Nov 3, 2014

I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits

Code:
class BitInput {
istream& in; // the istream to delegate to
char buf; // the buffer of bits
int nbits; public:

BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }

[Code] ....

View 4 Replies View Related

C :: File Reading And Writing

Sep 19, 2013

I have a text file containing 500 signed decimal numbers. My task is to read each entry and convert into a 16-bit 2's complement representation (binary number) and write into the another text file.

View 2 Replies View Related

C :: Reading And Writing To File

Mar 19, 2013

Code:
#include <stdio.h>
struct hardware{
int recNum;
char toolName[30];
int quant;
double cost;

[Code] .....

I'm having issues with my functions working properly. I'm not sure where I'm going wrong with them and if i'm using the correct mode for the fopen.

View 6 Replies View Related

C++ :: Reading And Writing To The Same File?

Apr 30, 2013

Is there a way to read and write to the same file?

I'm writing a game program and I want to save the score at the end of the game to a txt file. The txt file already contains other game scores. How do I store the score without overwriting the previous scores.

I'm just using basic fstream.

View 2 Replies View Related

C/C++ :: Reading And Writing Bit To A File

Nov 3, 2014

I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits

public:

BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
/** Read the next bit from the bit buffer.
* Return the bit read as the least significant bit of an int.
*/
int readBit(){
int i;
if(nbits == 8){
buf = in.get();

[Code] .....

View 2 Replies View Related

C++ :: Writing Data On A File In Binary Mode Multiple Times

Oct 20, 2013

When you have to write data on a file in binary mode multiple times (without closing the file), is the put pointer left where you ended writing the last time, or do you have to use tellp() or seekp() again and again to make sure you write at the right place?

I would have the same question about the get pointer, does he stay in place after you're done reading something (without closing the file, of course), or do you have to set it back at the right place with seekg() ??

View 5 Replies View Related

C++ :: Chat Messenger - Running Multiple Threads At Same Time

Sep 18, 2013

I'm doing work on chat messenger and facing difficulty to run multiple threads at the same time. Here is the sample code.

void menu();
void groupChat();
void personalChat();
int main() {
sf::Thread thread(&menu());
thread.launch();

[Code] ....

In my program, after menu when he selects a choice, next display of menu wait the termination of the selected thread.
while i want to show menu right after when a menu is selected.

View 2 Replies View Related

C++ ::  Reading And Writing To File Using Same Fstream

Oct 16, 2013

I have a file of data where each line consists of 4 parameters (degree, alpha, beta and x) and a value corresponding to these, organized in increasing order of the parameters. Here's the test file I'm using:

0 1 1 0.5 3.5
1 1 1 -0.5 -0.5
1 2 0 0.2 4.5
1 3 0 0.2 -4.5
2 0 0 1.5 2.1
2 1 0 0 5.6

My code is supposed to look for a certain set of parameters (currently set to 1 2 1 1.5, which is not contained in the file) and its value. If it's there (i.e. the file contains the value for the right parameters) the program is done. If not it should calculate the value using a function JacobiPoly() and insert the wanted parameters and corresponding value in the appropriate place in the file.

I have a code in which I think everything works except the writing to file. I use an fstream so I can both read and write and open it with ios::out||ios::in. I'm also aware I need something similar to fseek in between input and output but according to [URL] .... reading and writing to the same file using the same fstream, seekp() should suffice. I'm using seekp(), but the output (in the form inout << (...)) doesn't work.

Here's the code:

fstream inout;
inout.open("inouttest.dat", ios::out | ios::in);
int degree=1, readDeg;
double alpha=2, beta=1, x=1.5, value, readAlpha, readBeta, readX, readVal;
bool foundVal=false;
streampos beforeRead=(streampos)0;

[Code] ....

View 3 Replies View Related

C/C++ :: Reading And Writing PPM File Image

Dec 7, 2014

I am attempting to read a ppm file. When i do it i try to write it back in another file just to see how's done and i get a terrible result. I assume the problem is something with the casting i do to the variables.

This is my image class

Image:: Image(unsigned int width, unsigned int height, bool interleaved) {
buffer = new Component [3*width*height];
this->height=height;
this->width=width;
this->buffer=Component();

[Code] .....

View 4 Replies View Related

C :: Writing Binary Data Or Reading From A File

Mar 6, 2013

I am having problems either writing data to a binary file or reading from the file. Through the process of elimination I am posting the code where the data is written to file to see if I can eliminate that as an option. I know the data is being processed correctly because, through the use of another function, I can view the data.

I also know that fwrite must be including some padding because the file size ends up being 576 bytes after it is written instead of 540 bytes (the size it would be if no padding is used). Here is my struct:

Code:

typedef struct {
char teams[25];
float wins;
float losses;
float pct;
int runsScored;
int runsAgainst;
} STATISTICS;

Here is where I initialize it:

Code:
STATISTICS stats[12] = {
{"Anaheim Arrays", 0.0, 0.0, .000, 0, 0},
{"Pittsburg Pointers", 0.0, 0.0, .000, 0, 0},
{"Indianapolis Integers", 0.0, 0.0, .000, 0, 0},

[Code] ....

And here is the function that writes my data. The sA array is only used to change the scheduled games based on the variable week.

Code:
void schedule(STATISTICS stats[]) {
FILE *f;
int sA[12], week = 0, runsPerGameA = 0, runsPerGameB = 0, runsAgainstA = 0, runsAgainstB = 0;
int index, a = 0, b = 1, i = 0;

[Code] .....

View 13 Replies View Related

C++ :: Reading And Writing Data In Text File

Jan 5, 2015

Code to write data(Double type e.g 12345.67891) in text file like pattern given below. Remember to put tab between each column.

-----------------------------------------------------
Column1 Column2 Column3
Value 1 Value 2 Value 3
Value 4 Value 5 Value 6
Value 7 Value 8 Value 9
----------------------------------------------------

& Also how to read data from this file.

View 3 Replies View Related

C++ :: Reading File Then Writing To It If Duplicate Not Found

Jan 22, 2014

I'm having issues with writing to a file. more precisely, i'm trying to determine if a set of numbers are in the file. if they are already in it, don't write to the file. if not, write to the file. My problem is my program is writing all numbers, even duplicates. The sort is working.

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
//Counter Stats
int STR;

[code].....

View 9 Replies View Related

C++ :: Writing Integer Array To File In Binary Form And Reading It

May 30, 2013

Both arrays arr and pointers are the same size. I am having problems reading pointers from file into a new int array.

FILE* ky_pt=fopen("stashedclient","ab");
write(fileno(ky_pt), pointers, sizeof(pointers) );
pointerindex=lseek(fileno(ky_pt), 0, SEEK_CUR );
printf("pointerindex after writing array %d

[Code] .......

View 2 Replies View Related

C++ :: Reading GLOBE File - Access Violation Writing Location 0x00000001

Jan 15, 2015

I am working Visual C++ 2012. I am trying to read a GLOBE file using the following code.

#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#include "cmath"
#include <errno.h>
using namespace std;
/* Conversion useful */

[Code] ....

I am getting the error in the if condition (bold).

View 12 Replies View Related

C :: Visual Studio 2012 - Writing Data Into TXT File / How To Stop Reading Char

Feb 23, 2013

I am using visual studio 2012.....in below code i m writing data in to a test.txt file but i dont know with which key file stop accepting char...i tried ctrl+z and ctrl+d but not working ....

Code:
#include<stdio.h>
#include<conio.h>
main(){
char ch;
FILE *ptr;

[Code] ....

View 7 Replies View Related

C :: Reading File Multiple Times?

Apr 5, 2014

so i have to read a text file with an unknown number of lines and allocate memory to for the number of items in the text file.currently i read the entire file(counting the number of lines). i allocate memory according to the number of lines read and then use fseek() with an offset of zero to allow for the second read .

View 5 Replies View Related

C++ :: Reading From File And Store Values Of Each Line In Multiple Variables

May 6, 2013

So I have this text file that I am trying to read from and store the values of each line in multiple variables.

Let's say my text file contains

AXSYZ3482 Tom 100 112

and my code below here works fine when the lines in the text file is separated by spaces.

while (fscanf(fp, "%s %s %d %d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

But let's say my file was to look like this instead.

AXSYZ3482:Tom:100:112

But if i try this below...

while (fscanf(fp, "%s:%s:%d:%d
", userID, name, &startLoc, &endLoc) != EOF) {
printf("%s %s %d %d
", userID, name, startLoc, endLoc);
}

It seem to store the entire line in userID including the ":". I want to ignore the ":"'s and store everything in between in respective varibles in the order specified above.

So first string in userID, then ignore the :, then second string in name, and ignore the next :, and so forth. How I can accomplish this?

View 2 Replies View Related

C :: Writing Array Into File And Reading Array From File

Apr 20, 2014

i've been trying to write array into file and read them into the same program again, but the output is all wrong.

Code:

#include<stdio.h>
int main()
{
FILE *fp;
char name[3][7];
int x;
}

[code]....

View 1 Replies View Related

C/C++ :: Writing Multiple Files In Nested Loop?

Jun 3, 2014

I would like to do something like this:

for (int i=0; i<5; i ++)
{
for (int j=0; j<5; j++)
{
//* CREATE A NEW FILE FOR WRITING * //
}
}

I don't know how to create a new file that doesn't get overwritten each time the loop runs.

View 13 Replies View Related

C++ :: Composition - Writing Programs In Multiple Files

Oct 23, 2012

So lately I've been writing multiple header and cpp files and I would always the same error when I finally #include headers in my main.cpp. This error

Code:
undefined reference to `...`

On Youtube videos I see people doing this and their work magically compiles correctly. I found out that when I #include the headers' cpp files instead, my programs would compile. I use Code::Blocks with GNU GCC compiler.

View 2 Replies View Related

C++ :: Reading And Writing Files?

Feb 20, 2015

I have to write a program that sorts names and grades from a file and output them to another.

For example:

Name: Peter Parker
Exam 1: 95
Exam 2: 90.625
Exam 3: 91.20

Name: Mary Smith
Exam 1: 65
Exam 2: 79.1234
Exam 3: 70.24

Becomes something like this in the output file:

Name: Parker, Peter
Average Score: 92.28
Grade: A

Name: Smith, Mary
Average Score: 71.45
Grade: C

I know I'm supposed to read the whole file, but I'm getting really confused on how to take the name of each student separately without recording Exam 1, 2, and 3. I'll be able to do the average score and grade on my own.

View 2 Replies View Related

C++ :: Login With TXT Writing / Reading

Oct 28, 2013

I have to make a program that asks for your username (any)to register it. When you type it it will say something. I want to create a txt file also. so this is what I have:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
int a;
{
ofstream myfile;

[Code] ....

now, how can I use the cout again to print something "Welcome to the program..."?

View 1 Replies View Related

C/C++ :: Reading And Writing Files

Jan 16, 2015

I'm messing around with reading and writing files. The first file creates a small txt file. Simple enough

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string name;
string desc;

[Code] ....

It does what it should. It creates a text file "items.txt" .... It reads as such:

dagger,a dagger,15,10,0,1,3,1,0,0,0,1,0,0,0,0,0,1

The second file is meant to read the file and place the data back into the variables. This happens, but the data crams itself into the first variable, and the rest of them collect the trash that's in memory.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string name,desc;

[Code ....

I need to get "dagger" into name, "a dagger" into description, and each value with their perspective variable. I'm sure I need some type of "separator". Hopefully I can use the comma. Before it's over, I will have about a hundred items that will need to be read into a class of items.

View 4 Replies View Related

C :: Program Reading / Writing Files

Dec 10, 2013

program that I am working on. I want to use fgets() in my program so I could handle multiple words from a text(to be able to handle spaces). I get a weird result when running the program.

Code: #include <stdio.h>
#include <stdlib.h>
//#include "kim.h"
#include <string.h>
[code]....

View 4 Replies View Related







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