I have been trying to use the function in order to read alternate data streams, however I have been able to read only the file name. I want to read the content of the Alternate Stream.
I have been using the code from the following link.
What should I do in order to read the contents of the alternate stream?
I tried reading the documentation and looked for examples but havent really found anything useful
The example code your question refers to uses BackupRead to enumerate the names of the alternate streams (if any). It skips the actual content of each stream by calling BackupSeek to move directly to the next header.
You could either modify the code so that it reads the content rather than skipping it, or you can use the names to open and read from each stream separately.
To open a stream in file a.txt whose stream name is xyzzy, use the filename a.txt:xyzzy.
Related
I got 5 files generated by a fortran code like this
longP=8
OPEN(unit=20,FILE="GMt_2.dat",ACTION="write",ACCESS='Direct',RECL=longP)
count1=1
do J=K,fact
READ(10,*)XA,XB,YA,YB,ZA,ZB,rho
call Grv('f',Nx,Ny,dimg,Dx,Dy,XO,YO,XA,XB,YA,YB,ZA,ZB,rho,G,elev,Svec)
do I=1,dimg
WRITE(UNIT=20,rec=count1)Svec(I)
count1=count1+1
end do
WRITE(*,*)J
end do
dim(2)=J-1
fact=fact+fact1
call flush(20)
CLOSE(20)
which returned with an unreadable file format, my professor said "its binary, machine code" My goal here is to concatenate the information in those 5 files in one array to perform some processing. how can I achieve this?.
The code you show writes the data using unformatted I/O and direct access. You'll need to read it using unformatted I/O as well. You could use direct access or, and this would be my recommendation, stream access (ACCESS='STREAM' in the OPEN statement.) Open each file in sequence, read the data and then write it using the same mechanism to your single file. Your question is ambiguous enough to not allow a more detailed response.
I want to do the following:
open and read and ASCII file
locate a substring (geographical coordinates)
create its replacement (apply corrections to the original coordinates)
overwrite the original substring (write in the original file the corrected coordinates).
The format of the ASCII file is:
$GPGGA,091306.00,4548.17420,N,00905.47990,E,1,09,0.87,233.5,M,47.2,M,,*53
I will paste here only the part of the code that is responsible for this operation:
opnmea = fopen (argv[1], "r+");
if (fgets(row_nmea, ROW, opnmea)==NULL){
if (strstr(row_nmea,"$GPGGA")!=NULL) {
sscanf(row_nmea+17, "%10c", old_phi);
sscanf(row_nmea+30, "%11c", old_lam);
sscanf(row_nmea+54, "%5c", old_h);
fputs();
}
}
What I do till now is to extract in a variable the old coordinates and I was thinking to use fputs() for overwriting the old with new values. But I could not do it. The other part of the code that is not here is computing the correct coordinates. My idea is to correct the rows one by one, as the fgets() function reads each line.
I would appreciate very much any suggestion that can show me how to use fputs() or another function to complete my work. I am looking for something simple as I am beginner with C.
Thank you in advance.
Patching a text file in place is not a good solution for this problem, for multiple reasons:
the modified version might have a different length, hence patching cannot be done in place.
the read-write operation of standard streams is not so easy to handle correctly and defeats the buffering mechanism.
if you encounter an error during the patching phase, a partially modified file can be considered corrupted as one cannot tell which coordinates have been modified and which have not.
other programs might be reading from the same file as you are writing it. They will read invalid or inconsistent data.
I strongly recommend to write a program that reads the original file and writes a modified version to a different output file.
For this you need to:
open the original file for reading opnmea = fopen(argv[1], "r");
open the output file for writing: outfile = fopen(temporary_file_name, "w");
copy the lines that do not require modification: just call fputs(row_nmea, outfile).
parse relevant data in lines that require modification with whatever method you are comfortable with: sscanf, strtok, ...
compute the modified fields and write the modified line to outfile with fprintf.
Once the file has been completely and correctly handled, you can replace the original file with rename. The rename operation is usually atomic at the file-system level, so other programs will either finish reading from the previous version or open the new version.
Of course, if the file has only one line, you could simply rewind the stream and write back the line with fprintf, but this is a special case and it will fail if the new version is shorter than the original. Truncating the extra data is not easy. An alternative is to reopen the file in write mode ("w") before writing the modified line.
I would recommend strtok(), followed by your revision, followed by strcat().
strtok() will let you separate the line using the comma as a delimiter, so you will get the field you want reliably. You can break up the line into separate strings, revise the coordinates you wish, and reassemble the line, including the commas, with strcat().
These pages include nice usage examples, too:
http://www.cplusplus.com/reference/cstring/strtok/
http://www.cplusplus.com/reference/cstring/strcat/?kw=strcat
I have a file with path names to files:
/my/path1
/my/path11
/my/path12
/my/path13
The file structure is that it has individual paths in each line. All I want to do is search for the existence of a string /my/path1 or anyother in the above file many times
I could think of 2 methods.
every time get file contents line by line and then search the string. Advantage is that the file can be of anysize and I dont need to worry about buffer overflow.
Load the contents into a buffer and search it using the buffer. But as I dont have control over the file size I should be cautious here.
What is the best approach? I am working in unix. Is there any in-build library commands in C that I can make use of for this purpose? Or how can I accomplish the same task using awk in C code.
If you use stdio it will do the buffering for you. You can change its operation by using the function setvbuf to buffer more than a single line. getline can by used to check line by line.
I think loading all the file in memory is not a good idea. Using fgets and strcmp is the best way, I guess.
Consider the following scenario: I am opening a tar file (say abc.tar.gz), writing the data, and before closing the file descriptor, I am trying to extract the same file.
I am unable to do so. But if I extract the file after having closing the fd, it works fine.
I wonder what could be the reason.
All files has a position where data is read or written. After writing to the file, the position is at the end. Trying to read will attempt to read from that position. You have to change the position to the beginning of the file with a function like lseek.
Also, you did open the file in both read and write mode?
Edit
After reading your comments, I see you do not actually read the file from inside your program, but from an external program. Then it might be as simple as you not flushing the file to disk, which happens automatically when closing a file. You might want to check the fsync function for that, or possible the sync function.
(read) takes in a string from stdin, parses it as an s-expression, and returns that expression. How do I do the exact same thing, except taking input from a file?
Any of these:
(call-with-input-file "foo" read)
(with-input-from-file "foo" read)
The first will open the file and apply read on the open port to read a value and finally close it. The second is similar, except that it applies the function on no arguments in a dynamic context where the current input is read from the file. There are a bunch of other ways to do this, but you'll need to ask a more specific question...
(BTW, in the current repository version, which will be released as 4.2.3 soon, there is a new file->list function that will read all sexpressions from the file and return a list holding all of them.)