I'm trying to to read from a TXT file and do some calculation and write it back to another TXT file but when I read the character it changes to ASCII number (ex : '1' convert to 50) and when I try to write it in another file it's the ASCII number. How can I change it to that character I want?
int wf=FileOpen("wf.txt",FILE_WRITE|FILE_ANSI|FILE_TXT);
int rf=FileOpen("rf.txt",FILE_READ|FILE_ANSI|FILE_TXT);
str_size=FileReadInteger(rf,INT_VALUE); //the TXT I read is 1234
str=FileReadString(rf,str_size);
StringToCharArray(str,data1,0,StringLen(str));
RandonNum[0]= str[1];
RandonNum[1]= str[2];
RandonNum[2]= str[3];
FileWrite(wf,str[1],str[2],str[3]); //the TXT I write is 505152
FileReadInteger() is reserved for binary type files.
Unfortunately this is not explicitly stated in the documentation.
Use FileReadNumber() to read a number from txt file. It will return the number as a double, but it can be cast to an integer using a type cast (int)double_value.
Related
I have the following text string that represents hexadecimal bytes that should appear in file to create.
str = "001104059419632801001B237100300381010A"
I want to create a file that contains the above string in order when I open the created file with a Hex editor I see the same bytes
When I run this script
File.open("out.dat", 'w') {|f| f.write(str.unpack('H*')) }
it creates the file out.dat and when I open this file in a Hex editor contains this
5B2233303330333133313330333433303335333933343331333933363333333233383330333133303330333134323332333333373331333033303333333033303333333833313330333133303431225D
and I would like the content when I open the file in Hex editor be the same a text string
001104059419632801001B237100300381010A
How can I do this?
I hope make sense. Thanks
You have to split the string by aligned bytes in the first place.
str.
each_char. # enumerator
each_slice(2). # bytes
map { |h, l| (h.to_i(16) * 16 + l.to_i(16)) }.
pack('C*')
#⇒ "\x00\x11\x04\x05\x94\x19c(\x01\x00\e#q\x000\x03\x81\x01\n"
or, even better:
str.
scan(/../).
map { |b| b.to_i(16) }.
pack('C*')
Now you might dump this to the file using e.g. IO#binwrite.
What is the idiomatic way to remove non-ASCII characters from file contents in D?
I tried:
auto s = (cast(string) std.file.read(myPath)).filter!( a => a < 128 ).array;
which gave me:
std.utf.UTFException#C:\D\dmd2\windows\bin\..\..\src\phobos\std\utf.d(1109): Invalid UTF-8 sequence (at index 1)
and s is dstring ; and:
auto s = (cast(string) std.file.read(myPath)).tr("\0-~", "", "cd");
which gives me:
core.exception.UnicodeException#src\rt\util\utf.d(290): invalid UTF-8 sequence
at runtime.
I am trying to parse (with the almost deprecated std.xml module) xml files in a unsupported encoding, but I am ok with removing the offending characters.
If you do anything to consider it a string, D tries to treat it as UTF-8. Instead, treat it as a series of bytes, so replace your cast(string) with cast(ubyte[]) and do the filter.
After reading and filtering it, you can /then/ cast it back into a string. So this should do what you need:
auto s = cast(string) (cast(ubyte[])(std.file.read(myPath)).filter!( a => a < 128 ).array);
I am working on a project using DirectSound and am trying to make it so that the header information for the sound file is passed to the processing method. WAV files have a RIFF chunkID at the start. In a call to the ProcessWaveFile method, I pass the filepath as well as the header information. The 'RIFF' chunkID is stored in a char. I need to be able to check individual characters of the chunkID against the actual file chunkID to ensure the file is correct. This is a snippet of my code:
if((waveFileHeader.chunkId[0] != chunkID[0]) || (waveFileHeader.chunkId[1] != chunkID[1]) ||
(waveFileHeader.chunkId[2] != chunkID[2]) || (waveFileHeader.chunkId[3] != chunkID[3]))
{
MessageBox(hwnd, L"ChunkID not in the right Format.", L"Error", MB_OK);
return false;
}
chunkId is the file's actual ID whereas chunkID is the ID passed through the function to check. As you can see I'm trying to handle it like an array here. The chunkId is stored in a char[]. Should I store the chunkID in an array too? How would I specify it?
bool ProcessWaveFile(char*, IDirectSoundBuffer8**, HWND, int, char, char, char, char, std::string, int, int);
Above is the header file line for the ProcessWaveFile method. The chunkID is specified as one of the chars. I could change it to char[] but the difficulty comes when actually calling the method. Here is an example call:
result1 = ProcessWaveFile("../terrain_sky/data/sound01.wav", &m_secondaryBuffer1, hwnd,
44100, 'RIFF', 'fmt ', 'WAVE', 'data', "WAVE_PCM_FORMAT", 16, 2);
How could I declare the array values containg RIFF in this call without disrupting the chain of variables?
If you are looking for a convenient way to check that the file starts with 'RIFF', there may be a more convenient way for you to find out. Since the ChunkID is 4 bytes long, you may want to store what you expect 'RIFF' in a datatype that is also 4 bytes long (instead of a char array of length 4), then parse the first four bytes as, for example, an int.
I have a wav file that starts with RIFF here, here's what number you get when you interpret the first four bytes as an int.
od -ci -N4 ./test.wav
0000000 R I F F
1179011410
So the option -ci first prints each byte as its ASCII equivalent (RIFF), and then prints each set of 4 bytes as an integer. So really, 1179011410 = 'RIFF' and the integer may be easier for you to check for.
But as an aside, I'm not really sure why you need to pass 'RIFF' into the ProcessWaveFile() function. If you only ever deal with RIFF files, you might get away with having that information stored in the ProcessWaveFile() function itself.
I have data in the below format in a .txt file:
parameter1=12345 parameter2=23456 parameter3=23456 and so on.. the list is a long one.
I have found a way to match the parameter1 and so on and replace it with some other number.
modified_file=File.read("modified_file.txt",)
modified_file=modified_file.to_s.sub(/#{parameter1}=/, "some text of your choice")
The above regular expression would only replace the value with parameter1= but I intend to change following parameter1=.
I want to write a regular expression which can match the data up to = and replace the data following that.
For Eg: I want to replace 12345 to abcde and 23456 to xyzab so the final result would be:
parameter1=abcde parameter2=xyzab and so on..
/(?<=parameter1=)\S+/
What you want is called a "lookbehind".
I have a huge file to read whose structure is:
[...]
(0,0,0,0,0): 5.00634e-33, 5.59393e-33, 6.24691e-33, 7.29338e-33,
(0,0,0,0,4): 7.77607e-33, 8.95879e-33, 9.65316e-33, 1.07434e-32,
(0,0,0,0,8): 1.20824e-32, 1.34983e-32, 1.49877e-32, 1.73061e-32,
(0,0,0,0,12): 1.919e-32, 2.15391e-32, 2.3996e-32, 2.67899e-32,
[...]
I'm interested in reading the value after ":", which format should I use in the read statement if I use Fortran90?
I've tried with
read(1,'("(",I6,",",I6,",",I6,",",I6,",",I6,"):",F10.4,F10.4,F10.4,F10.4)')idx1,idx2,idx3,idx4,idx5,dummy1,dummy2,dummy3,dummy4
But I got a forrtl: severe (64): input conversion error
Since it appears that the items don't line up in columns this is tricky to do with formats. I'd approach it this way:
read (55, '(A)') string
colon_pos = index (string, ":")
read (string (colon_pos+1:len_string), * ) real1, real2, real3, real4
read each line into a string, locate the colon, then use list-directed IO to process the numeric values in the string after the colon.