io

Reading multiple lines with different data types in C

白昼怎懂夜的黑 提交于 2020-01-25 19:59:06
问题 I have a very strange problem, I'm trying to read a .txt file with C, and the data is structured like this: %s %s %d %d Since I have to read the strings all the way to \n I'm reading it like this: while(!feof(file)){ fgets(s[i].title,MAX_TITLE,file); fgets(s[i].artist,MAX_ARTIST,file); char a[10]; fgets(a,10,file); sscanf(a,"%d %d",&s[i].time.min,&s[i++].time.sec); } However, the very first integer I read in s.time.min shows a random big number. I'm using the sscanf right now since a few

Possible to get the file-path of the class that called a method in Java?

隐身守侯 提交于 2020-01-25 14:30:13
问题 Let's say for instance I have this scenario C:\Users\Name\Documents\Workspace\Project\Src\Com\Name\Foo.java Public class Foo { public Foo() { Bar b = new Bar(); b.method(); } } Then lets say that class Bar is in a .JAR file that's being used as a library, is it possible to figure out where the class that called method() was from? (in this case, the Foo class) I've done a little looking around Google and can't find anything, and this code would definately simplify my library quite a bit. 回答1:

Check If File Is In Use By Other Instances of Executable Run

自作多情 提交于 2020-01-25 13:06:31
问题 Before I go into too detail, my program is written in Visual Studio 2010 using C# .Net 4.0. I wrote a program that will generate separate log files for each run. The log file is named after the time, and accurate up at millisecond (for example, 20130726103042375.log ). The program will also generate a master log file for the day if it has not already exist (for example, *20130726_Master.log*) At the end of each run, I want to append the log file to a master log file. Is there a way to check

C# how to clear the streamwriter buffer without writing to a txt file?

偶尔善良 提交于 2020-01-25 12:08:07
问题 I am working on C# on Win7. I need to use Streamwriter to write to a txt file. StreamWriter outfile = new StreamWriter(MY_PATH, true); foreach(a line of strings) { // process the line outfile.Write(String.Format(WIDTH + " " + WIDTH, num1Str+"\t", num2Str+"\t")); } if all elements in line are "0" // do not write anything to the file, clear outfile buffer // WIDTH are constants. num1Str and num2Str are variables. How to clear the contents written in the stream buffer ? Flush is not a solution

Read uint32_t values from file

流过昼夜 提交于 2020-01-25 10:02:05
问题 I'd like to read uint32_t integers from a file using the code below. ifstream only accepts a pointer to a char array. Is there another way to read uint32_t values using a code similar to the below? int readCount; uint32_t buffer[SIZE]; while ( fin.read( &buffer[0], SIZE) || (readCount = fin.gcount()) != 0 ) { // some code } 回答1: Use a cast, e.g.: if (fin.read(reinterpret_cast<char *>(buffer), sizeof buffer) && fin.gcount() == sizeof buffer) { // use buf } (Interpreting any object as an array

Bash Read Escaped Characters and Normal Characters

主宰稳场 提交于 2020-01-25 00:09:06
问题 I want to read in bash when any key is pressed , be it arrow key, number, letter, or punctuation, without pressing enter. This is the closest that I have come up with, besides that when an escaped key is pressed, it spills over into the next input. Also, escaped keys will read but not echo. #!/bin/bash read -r -n 1 -d $'\n' -p 'input> ' line echo -en "\n" echo "$line" 回答1: A bit hacky/dirty way, but should work for user interactive shells... read -n1 -s -p 'input> ' line; read -t 0.1 -n2 -s

Guava how to copy all files from one directory to another

只愿长相守 提交于 2020-01-24 18:59:25
问题 I have been experimenting with guava lately and I have not come across a way to copy all of the files from one directory to another. If anyone can point me in the right direction on how to do this that would be great. 回答1: Guava is not providing any fancy file manipulation utilities apart from the rather rudimentary functionality in Files. To copy files from one directories, please use a different library, e.g. commons.io.FileUtils.copyDirectory. 来源: https://stackoverflow.com/questions

Guava how to copy all files from one directory to another

夙愿已清 提交于 2020-01-24 18:59:25
问题 I have been experimenting with guava lately and I have not come across a way to copy all of the files from one directory to another. If anyone can point me in the right direction on how to do this that would be great. 回答1: Guava is not providing any fancy file manipulation utilities apart from the rather rudimentary functionality in Files. To copy files from one directories, please use a different library, e.g. commons.io.FileUtils.copyDirectory. 来源: https://stackoverflow.com/questions

Convert R read.csv to a readLines batch?

ぐ巨炮叔叔 提交于 2020-01-24 17:26:06
问题 I have a fitted model that I'd like to apply to score a new dataset stored as a CSV. Unfortunately, the new data set is kind of large, and the predict procedure runs out of memory on it if I do it all at once. So, I'd like to convert the procedure that worked fine for small sets below, into a batch mode that processes 500 lines at a time, then outputs a file for each scored 500. I understand from this answer (What is a good way to read line-by-line in R?) that I can use readLines for this. So

How to force file flushing

Deadly 提交于 2020-01-24 17:00:51
问题 Suppose that I have the following code: #include <chrono> #include <fstream> #include <thread> int main() { std::ofstream f("test.log"); int i = 0; while (true) { f << i++; f.flush(); std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } (note that I have a flush call after each write operation) I noticed that this application doesn't update "last modified time" and "size" attributes of the "test.log" file unless I do a right-click on this file or open it. I guess that this is due