C++ :: Read In String From File And Convert It Into Enum?

Dec 2, 2013

I defined

enum boundaryType_t {inside, inlet, outlet, wall, periodic};

now I have file written like this:

inside outlet

I want my program read the file and when encounter any enum type, instead of treating it as a string, I want the program store it in memory as a enum value.

How can I do that?

string s;
ifs>>s;
s // how to convert it to enum???

View 5 Replies


ADVERTISEMENT

C/C++ :: Convert Enum To Text String

Jan 25, 2015

I want to convert an enum to a text string.

enum BREED {YORKIE,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB};

View 4 Replies View Related

C/C++ :: Read A Raw File And Convert It To 8 Bit

Feb 19, 2014

I am trying to rewrite a matlab code into c++. It reads a file and do the deresolution on it and convert it to 8 bit.

but the VC++ code does not work for all ranges of max_den and min_den. Specially when the there is a small difference between them. (I tried that range on Matlab, it works)

MATLAB Code:

fid = fopen(ORIGINAL_FILENAME,'r');
A = fread(fid,X*Y*Z,'int32=>int32')
D = uint8(255/double(Maximum_Density-Minimum_Density)*(A-Minimum_Density));
A = reshape(D,X,Y,Z);
fid=fopen(OUTPUT_FILENAME,'w');

[code].....

For example when actual range of data is [-3023,3072] and the max_den=-3000, min_den=1000, it works well. but when you choose max_den=-1000, min den=1000, it doesn't work properly.The raw file includes 3D data associated with a mhd file. When the range is not wide enough, the converted file becomes messy.

View 1 Replies View Related

C++ :: Using A String / Enum To Access Int Matrix?

Apr 2, 2012

Any way to use a string to access a specific item in a matrix of int[X].

I have a program which uses enums as iterators to reference a large amount of data. To select an item in the matrix, the user will enter a string, which is also an enum, which also must serve as an iterator for something in the matrix. Here is a toybox example:

#include <iostream>
#include <string>
using namespace std;
enum NyNumbers { First, Second, Third, Forth, LAST_VALUE };
int main(int argc, char* argv[]) {
int Matrix[LAST_VALUE] = { 1, 3, 7, 12 };

[Code] .....

The idea is the user executes the program by typing "./RUN First" to print out the first element in the MyNumbers array, "./RUN Second" to access the second, and so on. I can't use static numbers for the iterator. (i.e., "./RUN 1") I must reference by enum/string.

When run, the output of this problem is thus:

====================================================================
user@debian$ ./RUN Second
Matrix[ atoi(Second) ]: 1
user@debian$
====================================================================

BTW, if I change line 12 to this

====================================================================
cout<<Matrix[ argv[1] ]<<"
";
====================================================================

The error message is this

====================================================================
user@debian$ make
g++ -g -Wall -ansi -pg -D_DEBUG_ Main.cpp -o exe
Main.cpp: In function `int main(int, char**)':
Main.cpp:12: error: invalid types `int[4][char*]' for array subscript
make: *** [exe] Error 1
user@debian$
====================================================================

Which isn't unexpected. How to input the enum as argv[1]?

View 2 Replies View Related

C++ :: Converting Enum To String And Vice Versa?

Feb 25, 2014

It is working:

#include <iostream>
#include <string>
#include <vector>
#include <map>
const int ENUM_NOT_FOUND = -1; const std::string NEW = " ";
enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

[code]....

Ouput with GCC 4.8.1:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Name a day: Friday
day = Friday

But the problem is that whenever I define a new enum, I have to define the << and >> overloads for the new enum again. Isn't there a way to template that as well, so that the << and >> overload needs to be defined just once? My atttempt:

template<typename Enum>
std::ostream& operator << (std::ostream& os, Enum en) {
return os << EnumConversions<Enum>::toString (en);

[Code] .....

fails to compile. I guess the problem is Enum is not known at compile time, even though it should be deducible during run time? Error mentions ambiguous overload for operator>>.

View 6 Replies View Related

C/C++ :: Getting Errors On String And Enum Variable Declarations?

Oct 13, 2014

I need to do this simple Battleship game project for my C++ class. Here is my current code so far:

#include <iostream>
#include <array>
#include <string>
using namespace std;
enum BShip{ EMPTY, BTLSHIP, HIT, MISS };
const int BSIZE = 6;
const int BTLSHIP_SIZE = 4;
class Battleship {
private:
array<array<Bship, BSIZE>, BSIZE> sea, hits;

[code]....

For some reason, I'm getting errors on the string and enum declarations.

View 10 Replies View Related

C++ :: Not Able To Read Data From Text File And Convert It To Integer

Mar 25, 2013

I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:

20 40 20 25 30

20 30 40 20 25

20 40 20 25 30

20 30 40 20 25

30 40 40 30 40

I am using getline, and then converting it to int by using atoi. But my code is not working.

Code :
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code] ....

View 10 Replies View Related

C++ :: Read Byte Of Char Data And Convert It Into Text String Of Binary Data That Represents Hex Value

Dec 26, 2013

I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...

i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....

------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";

[Code] ....

When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.

The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.

I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.

When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..

input = input / BASE; I get C9 where I expected to get 48.

My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?

View 6 Replies View Related

C/C++ :: Convert File Name Into A String?

Feb 18, 2014

there is a way to convert a file's name into a string.

Sample:

filename = testcode.txt

it will convert the file's name into a string:

char convert[100];

covert = name of file

Is this possible?

View 3 Replies View Related

C++ :: Invoke Enum Class From Header File To Cpp File

May 21, 2014

I have been working a project in C++. I have TTTMain.cpp file that has all the function calls, TTTFuntions.cpp that has all the functions, I have TTT.h file that has all the prototypes and variables and additionally I have Winner.h that has enum class Winner declaration in it. Here is my block of codes:

Winner.h file:

#ifndef winner
#define winner
enum class Winner {

[Code]....

My question is when I compile this gives me error on

Winner gameSquares[] = { Empty, Empty,Empty, Empty, Empty, Empty, Empty, Empty, Empty };

with saying "invalid use of non-static data data member" and It says "Empty was not declared in this scope."

I know calling enum is very very trick.

View 3 Replies View Related

C++ :: Read String From The File?

Sep 23, 2014

I have an example text file that contains:

***xyz text********
***********<td>temperatura: </td><td class="data">19.8 °C</td>
***xyz text********
***********

How can i extract the number 19.8 from that file?

View 3 Replies View Related

C++ :: Read String In File And Add Count

Jul 22, 2013

I'm looking for a program to read a file to look for strings and add a count.

kill.log in the same folder as program

player1 was killed by player2
player2 was killed by player1

4 strings
player1 was for "d1" add count
player2 was for "d2" add count
by player1 for "k1" add count
by player2 for "k2" add count

cout << Player 1 kills: << k1 << deaths << d1;
cout << Player 2 kills: << k2 << deaths << d2;

add up each time.

View 7 Replies View Related

C/C++ :: Can't Read In String From Text File

Feb 17, 2014

I'm trying to make a program that will read in names and grades from a text file and print them in the console. However whenever I try to use the OpenFile.get function I get an error saying that there is "no instance of overloaded function"

getting this error resolved before I can.

my code so far (I know it's missing a lot, but that's not what I'm worried about right now.)

#include <fstream>
#include <iostream>
#include <string>

[Code]....

View 7 Replies View Related

C++ :: How To Read All Content From A Text File Into A String

Sep 24, 2013

I'm trying to read all content from a text file into a string. This is the code I'm using for it (I know there are other, maybe better ways to do it but I'd like to stick to this for now):

char* buffer;
std::string data;
FILE* fp = fopen("textfile.txt", "rb");
if (!fp) {
printf("Can't open file");

[Code] ....

So my problem is that I get an exception when I try to free the memory -> 0xC0000005: Access violation reading location 0x51366199

I even get the exception when I try to free it immediately after calloc() but why this is.

And if I use buffer = (char*)malloc(lSize); I don't get any exceptions.

So, why this fails and what I'm doing wrong.

View 14 Replies View Related

C Sharp :: How To Read And Split File As String

Apr 16, 2013

how to do this in C#. Need to connect to Oracle db, take a file from directory then read every line of the file. Lines are like this:

123234847656|8800359898818177|A|20130401 14:51:42|
123234847212||D|20130401 14:52:08|
123234847212||M|20130401 14:55:38|

Then will split as string on every '|' char and according to this flag |A|, |D| or |M| I will add/delete/modify information inside. I have trouble with this part with the connection and read/split file and check for the flag A, D or M.

View 2 Replies View Related

C/C++ :: Read From File / Assign As String And Call Function As Argument

Oct 29, 2014

I try to use passing function as argument but I'm stuck. I have two questions: First, I try to call uppercase and open .txt in tfm Second, How can I read characters from in.txt as string and assign to char content[] ?

#include <stdio.h>
void tfm( char str_filename[], void(*pf_convertion)( char content[]));
void uppercase(char content[]); //converts all letters to uppercase
int main(){
puts("-------------------------------");
printf("tfm:
");
tfm("in.txt", uppercase);

[Code] ....

View 2 Replies View Related

C++ :: Program To Read Middle Names Within Text File - Full String Not Appearing

Oct 24, 2014

I've a problem here...

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])

[Code] ....

The program needs to be able to read the middle names within the text file named Info2.txt

Simply adding the Mname or any related value where the other to name values doesn't do anything and only the 1st line appears on the output.

Don Jon111012
Jack Bo Todd151015
Jill Jo Jane161011
Bob Jack Chuck 131513
Da Fu111012

That is the correct way it appears in the text file. Also I know it's commented out, but that just to keep the program from crashing.

current output

Survey Results:

Name Results
Don Jon 33.00

This is the Info1.txt file info by the way.

Ken A202017
Gus B151015
Bill C151512
Jara C151720
Lu E101510
Chow B171015

And the output

Survey Results:

Name Results
Ken A 57.00
Gus B40.00
Bill C42.00
Jara C52.00
Lu E35.00
Chow B 42.00

Which when ran with Info1 selected, it is the correct output I'm looking for. I've tried getline and other solutions and they didn't work.

View 1 Replies View Related

C# :: Unable To Implicit Convert Type Int To String Though Declared Variables As String

Mar 26, 2014

Ok, so I'm writing this code and when I build it keeps saying cannot implicitely convert type int to string even though I declared my variables as string. Why is it giving me this error?

private static string Repair()
{
string result="";
string beep;
string spin;
Console.WriteLine("Does your computer beep on startup?:(y,n)");

[Code]...

View 3 Replies View Related

C++ :: Convert And Return C Format String To String?

Jun 8, 2014

heres the function:

string ToString ( const char * format, ... )
{
char buffer[256];

[Code]....

these function do the same of the sprintf() function. but instead we use a variable for add the result, i want to return the result. when i use it:

string f;
f=ToString("hello world");
gives me several errors:
"error: crosses initialization of 'std::string f'"
"error: jump to case label [-fpermissive]"

View 7 Replies View Related

Visual C++ :: How To Convert UTF-8 String To Unicode String

Jun 15, 2013

I am using Visual Studio 2008. I just wonder if there are any library function in Windows SDK or MFC, or from third-parties, that can convert a UTF-8 string into Windows Unicode string(used in CString object).

Can MultiByteToWideChar or ATL String conversion macro like A2W to the conversion?

View 1 Replies View Related

C# :: Convert Dictionaries To Arrays To Read And Write Sequentially

Nov 27, 2011

Code:

// declare child arrays
public string[,] childcolor;
public string[,] childgeneo;
// create dictionaries
public Dictionary<string, int> colors = new Dictionary<string, int>();
public string color; // name for dictionary
public Dictionary<string, string> geneo = new Dictionary<string, string>();

[Code]...

I'm getting "field' is used like a 'type' error on jw.childcolor which causes other parts to error out.

How do I fix this & why am I getting the error?

I want to convert the dictionaries to arrays to read & write sequentially.

View 4 Replies View Related

C++ :: Getline And Atof - Convert Read Value Into Floating Number

Aug 3, 2012

Having an issue with the following code. I read in a decimal value from a text file, use atof (or strtod, either gives the same error) to convert the read value into a floating point number. When putting the output of atof in a double, it works fine. However when putting the output of atof into a float, the decimal places get lost. Putting the result in a double, then into a float gives the same result. This only happens when using getline.

I've tried using temp arrays to put the result of pch in, sprintfs and such to try and work around it, but whenever a value originates from the getline command, the float value always loses decimal places. I've also tried varying the precision in the printf statement, but its always the same. When I write the float result to a file via FILE.write in binary mode, the result comes out without the decimal places when I read it back in later.

The simple solution is to not use float and use double, but there are a number of reasons I'm using float to begin with and don't want to change that.

Side note, this code works fine on a windows machine, the error is coming from using g++ on a mac.

Code:
printf("Reading from File
");
ifstream FILE(argv[2],ios::in);
char BU[128];
char *pch;

[Code] ....

Code output :

Reading from File
338769.0109
338769.010900
338769.000000

View 4 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,

N,N'-dimethylethylenediamine.mol

This is the function that opens the file :

Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );

[Code] ....

The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.

View 9 Replies View Related

C/C++ :: Change Enum Type Variable To String Type Variable?

Aug 10, 2014

How to change an enum type variable to a string type variable?

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++ :: Convert A Value Into A String?

May 21, 2013

I would like to convert a value into a string, and extract a value from a string. And then call these functions in the main.

template<class T>
string toString (T value) // convert a value into a string {
stringstream ss;

[Code[ .....

Are above functions correct? And how should I call in main?

int main() {
const int SIZE=10;
toString<int> intValue(SIZE); //seems not right
toString<double> doubleValue(SIZE); // seems not right
}

View 3 Replies View Related







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