formatted-input

Initializing a vector from a text file

Deadly 提交于 2021-02-04 19:44:10
问题 I am attempting to write a program which can read in a text file, and store each word in it as an entry in a string type vector. I am sure that I am doing this very wrong, but it has been so long since I have tried to do this that I have forgotten how it is done. Any help is greatly appreciated. Thanks in advance. Code: #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { vector<string> input; ifstream readFile; vector<string>::iterator

Matlab input format

你说的曾经没有我的故事 提交于 2019-12-31 07:06:24
问题 I have input files containing data in the following format. 65910/A 22 9 4 2 9 10 4 1 2 5 2 0 4 1 1 0 65910/T 14 7 0 4 8 4 0 2 1 2 0 0 1 1 1 1 . . . I need to take the input where the first line is a combination of %d and %c with a / in between and the next four line as a 4x4 integer matrix. I need to perform some work on the matrix and then identify them with the header information. How can I take this input format in MATLAB? 回答1: Since your file contains data that may be considered

Does anyone actually use stream extraction operators?

ε祈祈猫儿з 提交于 2019-12-30 00:03:10
问题 I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful. I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string ). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++? Questions have been asked about safely overloading stream operators. What I wonder is if anyone does this in practice. Even for

Read numbers from file into a dynamically allocated array

拥有回忆 提交于 2019-12-24 00:58:26
问题 I need a function that reads grades (integers) from from file and returns a dynamically allocated array in which they are stored. This is what I have tried: int *readGrades() { int *grades; int x; scanf("%d", &x); grades = malloc(x * sizeof(int)); return 0; } However I don't get anything when I run the code. The grades are stored in file called 1.in : 29 6 3 8 6 7 4 8 9 2 10 4 9 5 7 4 8 6 7 2 10 4 1 8 3 6 3 6 9 4 and I run my program using: ./a.out < 1.in Can anyone tell me what I did wrong?

How to extract mixed format using istringstream

谁说我不能喝 提交于 2019-12-19 19:46:41
问题 Why does my program not output: 10 1.546 ,Apple 1 instead of 10 1 <empty space> here's my program: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string str = "10,1.546,Apple 1"; istringstream stream (str); int a; double b; string c, dummy; stream >> a >> dummy >> b >> dummy >> c; cout << a << endl; cout << b << endl; cout << c << endl; return 0; } Basically I am trying to parse the comma-separated strings, any smoother way to do this would help me

python scientific notation with forced leading zero

ぃ、小莉子 提交于 2019-12-17 20:53:02
问题 I want to have Python2.7 print out floating point numbers in scientific notation, forced to start with 0. For instance, assume a=1234567890e12 print '{:22.16E}'.format(a) 1.2345678900000000E+21 However, I want a print output that looks like: 0.1234567890000000E+22 Notice that the exponent is raised by one since the desired output is forced to a leading zero. How can I achieve this? Thanks. 回答1: Well, since what you want to do is not "standard" scientific notation, I'm not sure if there is a

Matlab input format

我只是一个虾纸丫 提交于 2019-12-02 09:33:31
I have input files containing data in the following format. 65910/A 22 9 4 2 9 10 4 1 2 5 2 0 4 1 1 0 65910/T 14 7 0 4 8 4 0 2 1 2 0 0 1 1 1 1 . . . I need to take the input where the first line is a combination of %d and %c with a / in between and the next four line as a 4x4 integer matrix. I need to perform some work on the matrix and then identify them with the header information. How can I take this input format in MATLAB? Since your file contains data that may be considered structured (or "formatted", if using MATLAB's terms), you can use the textscan function to read its contents. The

How to extract mixed format using istringstream

自古美人都是妖i 提交于 2019-12-01 18:05:20
Why does my program not output: 10 1.546 ,Apple 1 instead of 10 1 <empty space> here's my program: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string str = "10,1.546,Apple 1"; istringstream stream (str); int a; double b; string c, dummy; stream >> a >> dummy >> b >> dummy >> c; cout << a << endl; cout << b << endl; cout << c << endl; return 0; } Basically I am trying to parse the comma-separated strings, any smoother way to do this would help me immense. In IOStreams, strings (meaning both C-strings and C++ strings) have virtually no formatting

Use scanf with Regular Expressions

隐身守侯 提交于 2019-11-30 04:30:11
问题 I've been trying to use regular expressions on scanf, in order to read a string of maximum n characters and discard anything else until the New Line Character. Any spaces should be treated as regular characters, thus included in the string to be read. I've studied a Wikipedia article about Regular Expressions, yet I can't get scanf to work properly. Here is some code I've tried: scanf("[ ]*%ns[ ]*[\n]", string); [ ] is supposed to go for the actual space character, * is supposed to mean one

Does anyone actually use stream extraction operators?

假装没事ソ 提交于 2019-11-30 01:45:37
I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful. I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string ). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++? Questions have been asked about safely overloading stream operators . What I wonder is if anyone does this in practice. Even for something simple like reading input from a file in C++ I can't suggest using operator>> . It's too