Code::Blocks Forums

User forums => Help => Topic started by: scootergarrett on September 10, 2014, 04:34:19 am

Title: Putting Text on ClipBoard
Post by: scootergarrett on September 10, 2014, 04:34:19 am
So I have been loving CodeBlocks (13.12 now) for a few years now and Linux (mint16) for a few months.
Any ways I'm trying to get a program to put a string onto my main clipboard, which I can do fine from a terminal with something like:
~ $ echo "This Text TEST" | xsel -i -b
Which puts "This Text TEST" on the clip board. So when I write this to the terminal from C using the code:

Code
int main()
{

    system("echo \"This Text\" | xsel -i -b");

    return 0;
}

it puts "This Text" on the clipboard as expected and the program exits as expected. But then I can't re-run the program until I copy some other text. By can't re-run I mean the green 'build and run' arrow is still grey. http://imgur.com/d1mLElr  but the second I copy some random text 'build and run' comes alive again. So what is really running that little run arrow? I 'Press ENTER to continue.' and nothing. any Ideas, Thanks in advance

Title: Re: Putting Text on ClipBoard
Post by: Alpha on September 10, 2014, 06:15:34 am
[...]  but the second I copy some random text 'build and run' comes alive again. So what is really running that little run arrow? [...]
Not C::B related, but a code issue. See notes section (http://linux.die.net/man/1/xsel) (specifically about forking a child).  The arrow turns green once the process it launched has completed, which is working correctly on your system.

(Note: If you have questions on fixing the code, a programming board is more applicable than this forum.  (Note: I shudder whenever I see an unwarranted system() within code.))
Title: Re: Putting Text on ClipBoard
Post by: scootergarrett on September 11, 2014, 04:51:52 am
I got something working so for completeness I will post. I found that 'xclip' was very similar and would not hang up C::B, but now the data is not retained in the clipboard after ending the program, but that is better for development. here is my function to put time and voltage data on the clipboard so I can paste it into a 'CSV Voltage Source' in https://www.circuitlab.com/



Code
#define ClipBoardDataFile "../../ClipBoardDataFile.txt"

/// sudo apt-get remove xsel
/// Needed to install xsel
/// Help from http://linux.die.net/man/1/xclip
/// and http://www.hashbangcode.com/blog/using-xclip-copy-output-command-line-linux

/// Puts data set from T and V into main clip board so it can be easily
/// pasted into a CircuitLab.com CSV element
void CLCSVtoClipBoard(size_t N, double T[N], double V[N])
{
    FILE *fp;
    int k;
    char SystemCommand[128];

    fp = fopen(ClipBoardDataFile, "w");     // Open temp file for data

    // Put data into temp file //
    fprintf(fp, "%lf , %lf", T[0], V[0]);
    for(k=1;k<N;++k)
        fprintf(fp, "\n%lf , %lf", T[k], V[k]);

    fclose(fp);                             // Close temp file for data

    usleep(100);        // Pause for just a moment

    /// System command to put fine data into main clipboard ///
    sprintf(SystemCommand, "xclip %s -selection c", ClipBoardDataFile);
    system(SystemCommand);


    usleep(100);        // Pause for just another moment

    printf("Paste data before ending program\n");
    fflush(stdout);


    remove(ClipBoardDataFile);  // Delete temp file for house keeping


    return;
}

#define CLCSVtoClipBoard(T, V, N) CLCSVtoClipBoard(N, T, V)



#define L 100
int main()
{
    int k;
    double Time[L], Voltage[L];


    for(k=0;k<L;++k)
    {
        Time[k] = k / 100.0;
        Voltage[k] = sin(k/40.0);
    }

    CLCSVtoClipBoard(Time, Voltage, L);

    return 0;
}

 
Title: Re: Putting Text on ClipBoard
Post by: stahta01 on September 11, 2014, 03:57:05 pm
Read the rules!
http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)