iostream

Reading a single character from an fstream?

女生的网名这么多〃 提交于 2019-12-04 03:05:45
问题 I'm trying to move from stdio to iostream, which is proving very difficult. I've got the basics of loading a file and closing them, but I really don't have a clue as to what a stream even is yet, or how they work. In stdio everything's relatively easy and straight forward compared to this. What I need to be able to do is Read a single character from a text file. Call a function based on what that character is. Repeat till I've read all the characters in the file. What I have so far is.. not

What are the rules of the std::cin object in C++?

纵饮孤独 提交于 2019-12-04 02:33:33
问题 I am writing a small program for my personal use to practice learning C++ and for its functionality, an MLA citation generator (I'm writing a large paper with tens of citations). For lack of a better way to do it (I don't understand classes or using other .cpp files inside your main, so don't bother telling me, I'll work on that when I have more time), I am writing a function for each type of citation. I might break this down into a function for each reused code if I get more time. My

Print function for class c++

旧巷老猫 提交于 2019-12-04 02:08:07
I want to write a print function for a class AutoData that has information about cars in it. With this print function, I would ideally like to print out a vector that contains many different class objects. I have already written get functions for each element of the objects, but I am still a bit unsure of how to go about using those to write a function to print out the data in the following format: mpg:cylinders:displacement:horsepower:weight:acceleration:modelYear:origin:carName For example: 10.0:8:360.0:215.0:4615.:14.0:70:1:ford f250 10.0:8:307.0:200.0:4376.:15.0:70:1:chevy c20 11.0:8:318.0

Is printing a null-pointer Undefined Behavior?

我的梦境 提交于 2019-12-04 01:28:30
问题 When studying the sample code for this question I had assumed it was Undefined Behaviour which was preventing subsequent uses of std::cout from printing. But it turns out that attempting to print a null pointer caused std::ios_base::badbit and std::ios_base::failbit to be set in its stream state which was the real cause for its being non-operational. Because of this, I am now curious if it really is Undefined Behaviour to (attempt) to print a null-pointer. So here are my questions: Is it

Correctly pad negative integers with zeros with std::cout

怎甘沉沦 提交于 2019-12-04 00:26:56
I found this question already asked, but the answer everybody gives is std::cout << std::setw(5) << std::setfill('0') << value << std::endl; which is fine for positive numbers, but with -5, it prints: 000-5 Is there a way to make it print -0005 or to force cout to always print at least 5 digits (which would result in -00005) as we can do with printf? std::cout << std::setw(5) << std::setfill('0') << std::internal << -5 << '\n'; // ^^^^^^^^ Output: -0005 std::internal Edit: For those those that care about such things, N3337 ( ~c++11 ), 22.4.2.2.2 : The location of any padding is determined

Why does ostream prints `1` for a string defined as `volatile char[]`? [duplicate]

纵然是瞬间 提交于 2019-12-03 23:04:08
This question already has answers here : Closed 5 years ago . Why does std::cout convert volatile pointers to bool? (4 answers) Consider this (artificial) example: #include <cstdio> #include <iostream> int main() { volatile char test[] = "abc"; std::printf("%s\n", test); std::cout << test << "\n"; } Compiling it with GCC and running gives the following output: $ g++ test.cc $ ./a.out abc 1 As you can see printf prints the string correctly while cout prints 1 . Why does writing to cout produces 1 in this case? The only suitable overload of operator<< is that for bool , so the array is converted

Java how to read folder and list files in that folder in jar environment instead of IDE

馋奶兔 提交于 2019-12-03 17:00:34
my problem is that I create a folder(name is IconResources) under src , in IconResources there are many pictures. Directory is like this: ProjectName src package 1 package 2 IconResources(this is the target folder) I want to list all picture files name and do something with those pictures. And I found that File.list() only works in IDE, if I export project to a jar to make a standalone, it cannot work. So I searched and found that I should use inputstream and outputstream. Now I find i/o stream can works well for single file. But I want to use inputstream and outputstream to read folder

How to set maximum read length for a stream in C++?

房东的猫 提交于 2019-12-03 16:58:22
I'm reading data from a stream into a char array of a given length, and I'd like to make the maximum width of read to be large enough to fit in that char array. The reason I use a char array is that part of my specification is that the length of any individual token cannot exceed a certain value, so I'm saving myself some constructor calls. I thought width() did what I wanted, but I was apparently wrong... EDIT: I'm using the stream extraction operators to perform the extraction, since these are flat text files with values separated by whitespace. char x[4]; cin.width(4); cin >> x; cout << x;

Fastest way to create large file in c++?

别说谁变了你拦得住时间么 提交于 2019-12-03 16:11:06
Create a flat text file in c++ around 50 - 100 MB with the content 'Added first line' should be inserted in to the file for 4 million times using old style file io fopen the file for write. fseek to the desired file size - 1. fwrite a single byte fclose the file The fastest way to create a file of a certain size is to simply create a zero-length file using creat() or open() and then change the size using chsize() . This will simply allocate blocks on the disk for the file, the contents will be whatever happened to be in those blocks. It's very fast since no buffer writing needs to take place.

How can I override an C++ standard-library class function?

我是研究僧i 提交于 2019-12-03 14:21:42
How can I override a C++ standard-library class function? In my application, I use ofstream objects in many different places of code. And now I want to open files in a different permission mode in Linux, Ubuntu. But open function of ofstream has no parameter to specify the permission mode of the file it creats. Now I want to override open() function of ofstream class so it will get a parameter to specify the permissions for user access. For starters, to clarify your terminology, the STL usually refers to the subset of the C++ standard library containing the containers, iterators, and