C++ Read an input file
i'm having problems reading a file and saving the values.
So the input file contains:
3 5
W2 R3 W3 R4
And what i want is this:
F = 3
P = 5
char Ac = 'W','R','W','R';
int acc = 2,3,3,4;
I already have the F = 3 and P = 5 but i don't know how to separate the other Strings.
I hope you can help me!
c++ file input iostream
add a comment |
i'm having problems reading a file and saving the values.
So the input file contains:
3 5
W2 R3 W3 R4
And what i want is this:
F = 3
P = 5
char Ac = 'W','R','W','R';
int acc = 2,3,3,4;
I already have the F = 3 and P = 5 but i don't know how to separate the other Strings.
I hope you can help me!
c++ file input iostream
Is there a reason you are using parallel arrays rather than an array (or vector) of a struct?
– Thomas Matthews
Nov 12 '18 at 23:46
bc this is for virtual memory simulation, i have to read if is Write or Read and the number of Page and my first program was like that, instead of reading from a file, so i can see if it works, but if there is a better way is welcome
– Zeta Zav
Nov 13 '18 at 1:48
add a comment |
i'm having problems reading a file and saving the values.
So the input file contains:
3 5
W2 R3 W3 R4
And what i want is this:
F = 3
P = 5
char Ac = 'W','R','W','R';
int acc = 2,3,3,4;
I already have the F = 3 and P = 5 but i don't know how to separate the other Strings.
I hope you can help me!
c++ file input iostream
i'm having problems reading a file and saving the values.
So the input file contains:
3 5
W2 R3 W3 R4
And what i want is this:
F = 3
P = 5
char Ac = 'W','R','W','R';
int acc = 2,3,3,4;
I already have the F = 3 and P = 5 but i don't know how to separate the other Strings.
I hope you can help me!
c++ file input iostream
c++ file input iostream
asked Nov 12 '18 at 22:50
Zeta ZavZeta Zav
13
13
Is there a reason you are using parallel arrays rather than an array (or vector) of a struct?
– Thomas Matthews
Nov 12 '18 at 23:46
bc this is for virtual memory simulation, i have to read if is Write or Read and the number of Page and my first program was like that, instead of reading from a file, so i can see if it works, but if there is a better way is welcome
– Zeta Zav
Nov 13 '18 at 1:48
add a comment |
Is there a reason you are using parallel arrays rather than an array (or vector) of a struct?
– Thomas Matthews
Nov 12 '18 at 23:46
bc this is for virtual memory simulation, i have to read if is Write or Read and the number of Page and my first program was like that, instead of reading from a file, so i can see if it works, but if there is a better way is welcome
– Zeta Zav
Nov 13 '18 at 1:48
Is there a reason you are using parallel arrays rather than an array (or vector) of a struct?
– Thomas Matthews
Nov 12 '18 at 23:46
Is there a reason you are using parallel arrays rather than an array (or vector) of a struct?
– Thomas Matthews
Nov 12 '18 at 23:46
bc this is for virtual memory simulation, i have to read if is Write or Read and the number of Page and my first program was like that, instead of reading from a file, so i can see if it works, but if there is a better way is welcome
– Zeta Zav
Nov 13 '18 at 1:48
bc this is for virtual memory simulation, i have to read if is Write or Read and the number of Page and my first program was like that, instead of reading from a file, so i can see if it works, but if there is a better way is welcome
– Zeta Zav
Nov 13 '18 at 1:48
add a comment |
1 Answer
1
active
oldest
votes
You can read character by character, skipping whitespaces.
Or you could read in a string, then split the string:
std::string rw_text;
std::vector<char> Ac;
std::vector<int> acc;
//...
while (input_file >> rw_text)
const char letter = rw_text[0];
const int number = rw_text[1] - '0';
Ac.push_back(letter);
acc.push_back(number);
You could also use a struct to keep your letters and numbers together, like a pair.
struct Letter_Number
char letter;
int number;
friend std::istream& operator>>(std::istream& input, Letter_Number& ln);
;
std::istream& operator>>(std::istream& input, Letter_Number& ln)
input >> ln.letter;
input >> ln.number;
return input;
//...
std::vector<Letter_Number> database;
Letter_Number ln;
//...
while (input_file >> ln)
database.push_back(ln);
A structure will keep the letters associated with their numbers. In a parallel array, you could have different offsets or the pair won't line up.
Also, with having a structure, the letter and number will be on the processor's same data cache line, so your program will be more efficient. Otherwise the processor will have to load in the Ac array, get the value, then load in the acc array and load in the character; wasting time reloading the data cache.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271230%2fc-read-an-input-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can read character by character, skipping whitespaces.
Or you could read in a string, then split the string:
std::string rw_text;
std::vector<char> Ac;
std::vector<int> acc;
//...
while (input_file >> rw_text)
const char letter = rw_text[0];
const int number = rw_text[1] - '0';
Ac.push_back(letter);
acc.push_back(number);
You could also use a struct to keep your letters and numbers together, like a pair.
struct Letter_Number
char letter;
int number;
friend std::istream& operator>>(std::istream& input, Letter_Number& ln);
;
std::istream& operator>>(std::istream& input, Letter_Number& ln)
input >> ln.letter;
input >> ln.number;
return input;
//...
std::vector<Letter_Number> database;
Letter_Number ln;
//...
while (input_file >> ln)
database.push_back(ln);
A structure will keep the letters associated with their numbers. In a parallel array, you could have different offsets or the pair won't line up.
Also, with having a structure, the letter and number will be on the processor's same data cache line, so your program will be more efficient. Otherwise the processor will have to load in the Ac array, get the value, then load in the acc array and load in the character; wasting time reloading the data cache.
add a comment |
You can read character by character, skipping whitespaces.
Or you could read in a string, then split the string:
std::string rw_text;
std::vector<char> Ac;
std::vector<int> acc;
//...
while (input_file >> rw_text)
const char letter = rw_text[0];
const int number = rw_text[1] - '0';
Ac.push_back(letter);
acc.push_back(number);
You could also use a struct to keep your letters and numbers together, like a pair.
struct Letter_Number
char letter;
int number;
friend std::istream& operator>>(std::istream& input, Letter_Number& ln);
;
std::istream& operator>>(std::istream& input, Letter_Number& ln)
input >> ln.letter;
input >> ln.number;
return input;
//...
std::vector<Letter_Number> database;
Letter_Number ln;
//...
while (input_file >> ln)
database.push_back(ln);
A structure will keep the letters associated with their numbers. In a parallel array, you could have different offsets or the pair won't line up.
Also, with having a structure, the letter and number will be on the processor's same data cache line, so your program will be more efficient. Otherwise the processor will have to load in the Ac array, get the value, then load in the acc array and load in the character; wasting time reloading the data cache.
add a comment |
You can read character by character, skipping whitespaces.
Or you could read in a string, then split the string:
std::string rw_text;
std::vector<char> Ac;
std::vector<int> acc;
//...
while (input_file >> rw_text)
const char letter = rw_text[0];
const int number = rw_text[1] - '0';
Ac.push_back(letter);
acc.push_back(number);
You could also use a struct to keep your letters and numbers together, like a pair.
struct Letter_Number
char letter;
int number;
friend std::istream& operator>>(std::istream& input, Letter_Number& ln);
;
std::istream& operator>>(std::istream& input, Letter_Number& ln)
input >> ln.letter;
input >> ln.number;
return input;
//...
std::vector<Letter_Number> database;
Letter_Number ln;
//...
while (input_file >> ln)
database.push_back(ln);
A structure will keep the letters associated with their numbers. In a parallel array, you could have different offsets or the pair won't line up.
Also, with having a structure, the letter and number will be on the processor's same data cache line, so your program will be more efficient. Otherwise the processor will have to load in the Ac array, get the value, then load in the acc array and load in the character; wasting time reloading the data cache.
You can read character by character, skipping whitespaces.
Or you could read in a string, then split the string:
std::string rw_text;
std::vector<char> Ac;
std::vector<int> acc;
//...
while (input_file >> rw_text)
const char letter = rw_text[0];
const int number = rw_text[1] - '0';
Ac.push_back(letter);
acc.push_back(number);
You could also use a struct to keep your letters and numbers together, like a pair.
struct Letter_Number
char letter;
int number;
friend std::istream& operator>>(std::istream& input, Letter_Number& ln);
;
std::istream& operator>>(std::istream& input, Letter_Number& ln)
input >> ln.letter;
input >> ln.number;
return input;
//...
std::vector<Letter_Number> database;
Letter_Number ln;
//...
while (input_file >> ln)
database.push_back(ln);
A structure will keep the letters associated with their numbers. In a parallel array, you could have different offsets or the pair won't line up.
Also, with having a structure, the letter and number will be on the processor's same data cache line, so your program will be more efficient. Otherwise the processor will have to load in the Ac array, get the value, then load in the acc array and load in the character; wasting time reloading the data cache.
edited Nov 12 '18 at 23:57
answered Nov 12 '18 at 23:51
Thomas MatthewsThomas Matthews
44.2k1171122
44.2k1171122
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271230%2fc-read-an-input-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there a reason you are using parallel arrays rather than an array (or vector) of a struct?
– Thomas Matthews
Nov 12 '18 at 23:46
bc this is for virtual memory simulation, i have to read if is Write or Read and the number of Page and my first program was like that, instead of reading from a file, so i can see if it works, but if there is a better way is welcome
– Zeta Zav
Nov 13 '18 at 1:48