file-io

Find and replace string between quotes

老子叫甜甜 提交于 2020-01-30 02:33:11
问题 I'm reading a file and I would like to replace any text which appears between two double quotes like this: If file input is: Hi, I'm an example file! "Hi there example file." "I'm mostly just here to get this quote to be colored!" Output should be: Hi, I'm an example file! [color=x]"Hi there example file."[/color] [color=x]"I'm mostly just here to get this quote to be colored!"[/color] I've wrote these three modules to do that, the first two work but the last doesn't. Module 1: __author__ =

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

How to I get the counter to work in this C program?

风流意气都作罢 提交于 2020-01-25 08:14:11
问题 #include <stdio.h> #include <stdlib.h> int main() { FILE *myFile; int x, b, n = -1, i, count = 1; int num[101]; myFile = fopen("a.txt", "r"); b = fscanf(myFile, "%d", &x); while (b != -1){ n++; num[n] = x; b = fscanf(myFile, "%d ", &x); } for (int i = 0; i <= n; i++){ printf("%d ", num[i]); } printf("\n"); for(int i = 1; i <= 100; i++){ if(num[i] == i) { printf("%i has occurred: %d times\n", i, count); count++; } } fclose(myFile); } I have a project for a Unix and C programming class due on

What's the most efficient way to write large text file in java?

不想你离开。 提交于 2020-01-25 06:55:07
问题 I'm trying to write a file with some amount of data using this: public static <T extends SomeClass> void writeFile(String buffer, Class<T> clazz, int fileNumber) { String fileType = ".txt"; File file = new File(clazz.getName()+fileNumber+fileType); PrintWriter printWriter = null; try { FileWriter writer = new FileWriter(file); printWriter = new PrintWriter(writer); printWriter.print(buffer);//error occurs here printWriter.flush(); printWriter.close(); System.out.println("created file: "+file

Win32 API: how to read the serial, or exit within a timeout if wasn't a data

半城伤御伤魂 提交于 2020-01-25 06:36:47
问题 I need a function to read data from a serial port or return if none came within a time interval. E.g. on GNU/Linux you could use poll() or select() + read() . Anything analogous in Windows? Below is something I tried: it ought to work, but the function GetOverlappedResult() is either buggy or not well documented; instead of number of bytes read it reports nothing (not even zero, e.g. if I don't initialize the lpNumberOfBytesTransferred argument, it just contains junk) . // NOTE: the `file`

Win32 API: how to read the serial, or exit within a timeout if wasn't a data

随声附和 提交于 2020-01-25 06:36:24
问题 I need a function to read data from a serial port or return if none came within a time interval. E.g. on GNU/Linux you could use poll() or select() + read() . Anything analogous in Windows? Below is something I tried: it ought to work, but the function GetOverlappedResult() is either buggy or not well documented; instead of number of bytes read it reports nothing (not even zero, e.g. if I don't initialize the lpNumberOfBytesTransferred argument, it just contains junk) . // NOTE: the `file`

File handling with non-administrative user

泪湿孤枕 提交于 2020-01-25 00:32:15
问题 I've been testing my application to see how well it works when run by a non-administrative user, and I have found problems with file handling. I am getting an UnauthorizedAccessException when trying to overwrite a file, if the file was created by and adminstrative user. When writing out the file I first create the file as a .tmp file, then use File.Copy to overwrite the original. The .tmp file gets created, but File.Copy fails. My files are written to a public directory ("C:\Documents and

Negated scanset in fscanf and EOF

谁都会走 提交于 2020-01-24 17:54:49
问题 I've got a comma-separated list of strings in my file: Name 1, Name 2, Name 3, I want to read those names skipping all commas. I've written the following loop: while(true) { if(fscanf(file, "%[^,],", my_string) != 1) { break; } //... } However, it is always executing one more time than it supposed to. Given 3 names in the file, the loop will execute its statements 4 times. Why is this happening? Does EOF indicator rank to my negated scanset [^,] ? If so, then how can I solve this issue? 回答1:

Negated scanset in fscanf and EOF

北城余情 提交于 2020-01-24 17:54:26
问题 I've got a comma-separated list of strings in my file: Name 1, Name 2, Name 3, I want to read those names skipping all commas. I've written the following loop: while(true) { if(fscanf(file, "%[^,],", my_string) != 1) { break; } //... } However, it is always executing one more time than it supposed to. Given 3 names in the file, the loop will execute its statements 4 times. Why is this happening? Does EOF indicator rank to my negated scanset [^,] ? If so, then how can I solve this issue? 回答1:

ETXTBSY and how to override it

百般思念 提交于 2020-01-24 08:54:09
问题 I need to write to an executable file that is being executed, but I can't open it for writing. For example: #include <stdio.h> #include <fcntl.h> int main(int argc, char **argv) { int fd = open(argv[0], O_RDWR); if (fd == -1) perror(NULL); return 0; } % uname -rs FreeBSD 8.0-STABLE % ./example_ETXTBSY Text file busy There are some explanations what the heck is ETXTBSY in Linux, but nevertheless, is it possible to override this error? P.S. I'm not trying to write a virus. 回答1: If you are