|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On April 08 2012 07:16 Ultraliskhero wrote: Hi guys, I have a quick question.
I'm doing file i/o using C on a linux environment, and I am wondering if there is a way to delete the contents of an entire file that was opened earlier without closing the file.
Basicly, I first opened an existing file using the "r+" mode for fopen(), then I'm reading the file until EOF, and then I want to delete the contents of the file and write to it again starting from the begining. However, I cannot simply close the file and reopen it using the "w" mode for fopen() because I have the file locked using flock(), and I do not want to close (unlock) the file before I finish doing all the operations on it.
Thanks.
quick google: maybe freopen helps? http://www.cplusplus.com/reference/clibrary/cstdio/freopen/
|
On April 08 2012 07:16 Ultraliskhero wrote: Hi guys, I have a quick question.
I'm doing file i/o using C on a linux environment, and I am wondering if there is a way to delete the contents of an entire file that was opened earlier without closing the file.
Basicly, I first opened an existing file using the "r+" mode for fopen(), then I'm reading the file until EOF, and then I want to delete the contents of the file and write to it again starting from the begining. However, I cannot simply close the file and reopen it using the "w" mode for fopen() because I have the file locked using flock(), and I do not want to close (unlock) the file before I finish doing all the operations on it.
Thanks.
You can also check out ftruncate, though I don't know the details of it and how it will affect file I/O in the future.
http://linux.die.net/man/2/ftruncate
|
On April 08 2012 07:16 Ultraliskhero wrote: Hi guys, I have a quick question.
I'm doing file i/o using C on a linux environment, and I am wondering if there is a way to delete the contents of an entire file that was opened earlier without closing the file.
Basicly, I first opened an existing file using the "r+" mode for fopen(), then I'm reading the file until EOF, and then I want to delete the contents of the file and write to it again starting from the begining. However, I cannot simply close the file and reopen it using the "w" mode for fopen() because I have the file locked using flock(), and I do not want to close (unlock) the file before I finish doing all the operations on it.
Thanks.
Can't you work on temporary copy of file, and just replace file after all the changes?
|
I did originally work on a temporary copy of the file and then replaced the file after the changes, but I think there are some problems with that now when I have to implement file locking for concurrent processes. ( maybe there's some way to solve this but I'm not sure)
But I think ftruncate() is the function I need! Thanks a lot guys!
|
Anyone have any experience using the Process class in Java?
I am trying to start a C++ program from a Java program, and write input to the C++ program from the Java program. I have a bigger project in mind, but right now I'm just trying to get this to work on Hello World.
Here is my Java code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter;
public class JavaRuntimeTest {
/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { ProcessBuilder pb = new ProcessBuilder("runtimeTest.exe"); Process helloWorldCPP = pb.start(); InputStreamReader ir = new InputStreamReader(helloWorldCPP.getInputStream()); OutputStreamWriter outr = new OutputStreamWriter(helloWorldCPP.getOutputStream()); BufferedReader br = new BufferedReader(ir); PrintWriter pr = new PrintWriter(outr); System.out.println(br.readLine()); pr.println("q"); helloWorldCPP.destroy(); }
}
The C++ code is just Hello World, and works fine on its own.
When I run the Java program, nothing outputs to the Java console, but I know the C++ executable is running because I see it in my task manager. Why can't I read from or write to the C++ program?
|
Here's a small utility class of mine for this scenario: http://pastebin.com/FTbbx0AS
basically, what you have to do is start separate threads that actively poll the processes input streams (either that or you manually poll them round-robin, but that's silly unless you need synchronous processing). The class I linked simply pulls data from the given input stream and writing it to the given output channel until the input stream ends. For processes, simply use it like this to have it asynchronously print all stdout and stderr of the process to the stdout and stderr of your java process:
Process process = ....; new Thread(new InputStreamToPrintWriter(process.getInputStream(), System.out)).start(); new Thread(new InputStreamToPrintWriter(process.getErrorStream(), System.err)).start();
|
On April 11 2012 09:30 MisterD wrote:Here's a small utility class of mine for this scenario: http://pastebin.com/FTbbx0ASbasically, what you have to do is start separate threads that actively poll the processes input streams (either that or you manually poll them round-robin, but that's silly unless you need synchronous processing). The class I linked simply pulls data from the given input stream and writing it to the given output channel until the input stream ends. For processes, simply use it like this to have it asynchronously print all stdout and stderr of the process to the stdout and stderr of your java process: Process process = ....; new Thread(new InputStreamToPrintWriter(process.getInputStream(), System.out)).start(); new Thread(new InputStreamToPrintWriter(process.getErrorStream(), System.err)).start(); Alright. I was hoping to not need threads, but if that's how its gotta be then I guess that's what I'll do. Thanks for the class.
|
Hi everyone, i've got some questions about php.
I'm trying to edit a file with a php code, it's simple. But the fact is, the two files are in different folder.
It goes like this
file i want to edit : mrbs/theme.inc file where the code is : mrbs/theme/classic/header.inc
I can't change where the files are cause the web application won't work.
I've tried something like this :
<form method="post"> <p><input type="submit" value="Change theme" name="theme" /></p> </form> <?php mysql_connect('xx', 'xx', 'xx'); mysql_select_db('mrbs'); if (isset($_POST['theme'])) { $fp = fopen("/var/www/mrbs/theme.inc","w"); //addcode }?> The button is here if the user want to change the theme of the application, and then the php is appeal. The next step is the editing of the file by adding a line, this is the easy part. But when i hit the button the following message appears :
Warning: fopen(/var/www/mrbs/theme.inc): failed to open stream: Permission not allowed in /var/www/mrbs/Themes/default/header.inc on line 3310
Any help ? 
|
Do you have write/read access to that folder? And are you sure thats the correct folder to open a file on?
|
On April 11 2012 09:43 Millitron wrote:Show nested quote +On April 11 2012 09:30 MisterD wrote:Here's a small utility class of mine for this scenario: http://pastebin.com/FTbbx0ASbasically, what you have to do is start separate threads that actively poll the processes input streams (either that or you manually poll them round-robin, but that's silly unless you need synchronous processing). The class I linked simply pulls data from the given input stream and writing it to the given output channel until the input stream ends. For processes, simply use it like this to have it asynchronously print all stdout and stderr of the process to the stdout and stderr of your java process: Process process = ....; new Thread(new InputStreamToPrintWriter(process.getInputStream(), System.out)).start(); new Thread(new InputStreamToPrintWriter(process.getErrorStream(), System.err)).start(); Alright. I was hoping to not need threads, but if that's how its gotta be then I guess that's what I'll do. Thanks for the class.
well you *can* do without threads, but it becomes rather tedious because you then have to circle through all the streams manually. But you can do that if you want to. There's just no point in doing this programming overhead if you don't need the output for synchronous processing.
|
Hyrule18979 Posts
On April 11 2012 18:04 SnoWhiTe wrote:Hi everyone, i've got some questions about php. I'm trying to edit a file with a php code, it's simple. But the fact is, the two files are in different folder. It goes like this file i want to edit : mrbs/theme.inc file where the code is : mrbs/theme/classic/header.inc I can't change where the files are cause the web application won't work. I've tried something like this : <form method="post"> <p><input type="submit" value="Change theme" name="theme" /></p> </form> <?php mysql_connect('xx', 'xx', 'xx'); mysql_select_db('mrbs'); if (isset($_POST['theme'])) { $fp = fopen("/var/www/mrbs/theme.inc","w"); //addcode }?> The button is here if the user want to change the theme of the application, and then the php is appeal. The next step is the editing of the file by adding a line, this is the easy part. But when i hit the button the following message appears : Show nested quote +Warning: fopen(/var/www/mrbs/theme.inc): failed to open stream: Permission not allowed in /var/www/mrbs/Themes/default/header.inc on line 3310 Any help ?  fopen("../../theme.inc")
|
On April 11 2012 20:49 tofucake wrote:Show nested quote +On April 11 2012 18:04 SnoWhiTe wrote:Hi everyone, i've got some questions about php. I'm trying to edit a file with a php code, it's simple. But the fact is, the two files are in different folder. It goes like this file i want to edit : mrbs/theme.inc file where the code is : mrbs/theme/classic/header.inc I can't change where the files are cause the web application won't work. I've tried something like this : <form method="post"> <p><input type="submit" value="Change theme" name="theme" /></p> </form> <?php mysql_connect('xx', 'xx', 'xx'); mysql_select_db('mrbs'); if (isset($_POST['theme'])) { $fp = fopen("/var/www/mrbs/theme.inc","w"); //addcode }?> The button is here if the user want to change the theme of the application, and then the php is appeal. The next step is the editing of the file by adding a line, this is the easy part. But when i hit the button the following message appears : Warning: fopen(/var/www/mrbs/theme.inc): failed to open stream: Permission not allowed in /var/www/mrbs/Themes/default/header.inc on line 3310 Any help ?  fopen("../../theme.inc")
That might not be enough.
@Snowhite: Check the permissions of the file. It has to be writeable (rw- aka 6) for either the user or group the webserver is started as. Depending on the linux distribution it can be www,apache,apache2,httpd or httpd2.
Sidenote: To just add a line, you can use file_put_contents with FILE_APPEND as third parameter, it saves you the work of fopen(),fwrite(),fclose() and does all of that with only one command.
|
I've checked the permission before and it's all good (might check again, who knows), i've also try the ../../theme.inc and it doesn't work. And the application is working on localhost.
Unfortunalty I can't see right now what you're telling me, can only work at school but thanks anyway ! Will try it when I can.
|
On April 12 2012 00:27 SnoWhiTe wrote:I've checked the permission before and it's all good (might check again, who knows), i've also try the ../../theme.inc and it doesn't work. And the application is working on localhost. Unfortunalty I can't see right now what you're telling me, can only work at school but thanks anyway ! Will try it when I can. 
can you show the output of "ls -la /var/www/mrbs/theme.inc", "ls -lad /var/www/mrbs" and "ps aufx | grep httpd" or "ps aufx | grep apache"?
|
@ Above w/ the C++ & Java:
Depending on the scope of your need to communicate between C++ and Java, it may be worth investing some time into looking at JNI Bridge. Despite the fact that it may make you want to pull your hair out, if you are doing a lot of integration between the languages it'll potentially save you a lot of time.
If your need is pretty simple though (e.g. is limited to literally just stuffing one chunk of data at one single point in java to one single point in C++), then JNI Bridge will be overkill.
|
On April 12 2012 01:06 phar wrote: @ Above w/ the C++ & Java:
Depending on the scope of your need to communicate between C++ and Java, it may be worth investing some time into looking at JNI Bridge. Despite the fact that it may make you want to pull your hair out, if you are doing a lot of integration between the languages it'll potentially save you a lot of time.
If your need is pretty simple though (e.g. is limited to literally just stuffing one chunk of data at one single point in java to one single point in C++), then JNI Bridge will be overkill. I just need to start a C++ program and pass it a parameter from within a Java program. I may also want to get a little output from the C++ program, but I can probably do without it.
The Java program is a UI to enable easier access to a much larger C++ program. Right now, the C++ program is just run from command line, but the hope is that eventually non-CS people will be able to use it.
|
|
nice blog bro :D
the language you want to learn strongly depends on the field you want to get into. When doing web design, you'll probably learn tons of html, css, javascript, maybe mixed in with some server language like ruby, cgi or php. For flash game, you'd obviously learn action script (the "flash language"), for freelance software development you'd probably use java or c++. But there isn't one language you can just learn and go from there. The only reason to learn a "randomly chosen" language is to grasp the basics of programming rather than be able to do something productive with it.
so you should either think hard of what of these things you'd like to do most and go for that or check your local job opportunities with what you could find work or maybe a mix of both.
in any case, you don't need to be a uber wizard with a language to produce results. Design quality and production efficiency grow with knowledge and experience, but you can already produce results when you are a bad programmer. It might have more bugs, it will likely be harder to maintain and it will definitely take longer than someone with a lot of experience, you'll have to live through the beginning with that in mind. But making like a small website with just some html / php / ajax on it can to large parts be achieved by copy/pasting a bunch of code snippets obtained from tutorials and a few clever google searches. That's a tedious way to work obviously, but it very much is an attainable start for self-learning.
|
On April 12 2012 06:36 Millitron wrote:Show nested quote +On April 12 2012 01:06 phar wrote: @ Above w/ the C++ & Java:
Depending on the scope of your need to communicate between C++ and Java, it may be worth investing some time into looking at JNI Bridge. Despite the fact that it may make you want to pull your hair out, if you are doing a lot of integration between the languages it'll potentially save you a lot of time.
If your need is pretty simple though (e.g. is limited to literally just stuffing one chunk of data at one single point in java to one single point in C++), then JNI Bridge will be overkill. I just need to start a C++ program and pass it a parameter from within a Java program. I may also want to get a little output from the C++ program, but I can probably do without it. The Java program is a UI to enable easier access to a much larger C++ program. Right now, the C++ program is just run from command line, but the hope is that eventually non-CS people will be able to use it.
I recommend rerunning this with an executable that you know works. (Just to debug - make sure you're actually starting the exe so you can get some visual output) Try starting up IE or something basic, and stepping through your code. I'm unfamiliar with Java, but I've done this a lot with System.Diagnostics in .Net (Process and ProcessStartInfo classes) and I've been able to capture stdOUT and stdERR reliably.
This may be blatantly wrong (again unfamiliar with java), but doesnt your EXE spawn its own thread and execute when you call process.start() ? This means you're assigning output / input streams after the program has run. Just a guess.
Edit: Because of the class names 'Process' and 'ProcessBuilder' I'm assuming it functions similar to the .NET classes.
|
On April 12 2012 07:27 ObliviousNA wrote:Show nested quote +On April 12 2012 06:36 Millitron wrote:On April 12 2012 01:06 phar wrote: @ Above w/ the C++ & Java:
Depending on the scope of your need to communicate between C++ and Java, it may be worth investing some time into looking at JNI Bridge. Despite the fact that it may make you want to pull your hair out, if you are doing a lot of integration between the languages it'll potentially save you a lot of time.
If your need is pretty simple though (e.g. is limited to literally just stuffing one chunk of data at one single point in java to one single point in C++), then JNI Bridge will be overkill. I just need to start a C++ program and pass it a parameter from within a Java program. I may also want to get a little output from the C++ program, but I can probably do without it. The Java program is a UI to enable easier access to a much larger C++ program. Right now, the C++ program is just run from command line, but the hope is that eventually non-CS people will be able to use it. Ok, so I recommend redoing this with an executable that you know works. Try starting up IE or something basic, and stepping through your code. I'm unfamiliar with Java, but I've done this a lot with System.Diagnostics in .Net (Process and ProcessStartInfo classes) and I've been able to capture stdOUT and stdERR reliably. This may be blatantly wrong (again unfamiliar with java), but doesnt your EXE spawn its own thread and execute when you call process.start() ? This means you're assigning output / input streams after the program has run. Just a guess. Edit: Because of the class names 'Process' and 'ProcessBuilder' I'm assuming it functions similar to the .NET classes.
The process does start in a separate thread, hence you are assigning input and output streams _while_ the other process is running, not after. But any output occurring is buffered until you actually read it, so you don't miss anything. In fact, some programs you run this way will block because they are either waiting for input or because output buffers just run full and so their print to stdout gets blocked until you start reading from these streams.
|
|
|
|