Page 1 of 2 12 LastLast
Results 1 to 16 of 17

Thread: C Programming Help

  1. #1
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    C Programming Help

    Please read the last post as I am updating this thread with all my questions as they develop rather than starting a new thread each time

    =======================================================================
    [Steve has solved the program open problem]

    Last time I really understood and did c programming was about 3 years ago at the start of my degree.

    Background to my project: I need to create a program that will cause optimisation of the key parameters of an already made simulation program. Essentially I need to create a program/some code to link these 2 separate programs together to optimise and re-run the simulation until the whole parameter set is optimised.

    I'm sure I will hit many a brick wall whilst renewing my interest in the language, so here goes my first question.

    Suppose I have a simulation program called anything.exe in the current directory i am working in. For the simulation program to open it needs to run in the simulation parameters and values from a text document (although it does not need the .txt etc)
    The file name to open with the simulation can be any word, suppose it is called hexus

    If I go to command prompt, i can run the program and get it to automatically run the file by going to the correct directory and typing the following
    C:\DIR>anything.exe hexus

    How can I open this program (in the same directory) and enter the character string obtained from the input?
    [basically doing what cmd prompt does, it needs to beopened automatically as due to the algorithm this process may need to be repeated 1000 times or more ]

    Many thanks

    [It seems so simple on first glance but I can't seem to find any reference on it -and Kernighan and Ritchie don't seem to be able to help me ]

    PS. If it makes any difference I/'m using DEVCPP as my compiler.
    Last edited by muddyfox470; 15-01-2009 at 02:31 PM.
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  2. #2
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C Programming Help

    Quote Originally Posted by man 2 chdir
    NAME
    chdir, fchdir - change working directory

    SYNOPSIS
    #include <unistd.h>

    int chdir(const char *path);
    int fchdir(int fd);

    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

    fchdir(): _BSD_SOURCE || _XOPEN_SOURCE >= 500

    DESCRIPTION
    chdir() changes the current working directory of the calling process to the directory specified in path.

    fchdir() is identical to chdir(); the only difference is that the directory is given as an open file descriptor.

    RETURN VALUE
    On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

    ERRORS
    Depending on the file system, other errors can be returned. The more general errors for chdir() are listed below:

    EACCES Search permission is denied for one of the components of path. (See also path_resolution(7).)

    EFAULT path points outside your accessible address space.

    EIO An I/O error occurred.

    ELOOP Too many symbolic links were encountered in resolving path.

    ENAMETOOLONG
    path is too long.

    ENOENT The file does not exist.

    ENOMEM Insufficient kernel memory was available.

    ENOTDIR
    A component of path is not a directory.

    The general errors for fchdir() are listed below:

    EACCES Search permission was denied on the directory open on fd.

    EBADF fd is not a valid file descriptor.
    That should do it. Although I would rather modify anything.exe to accept an optional directory argument than doing what is essentially 'shell scripting' in C, but if you don't have access to the source code for that, then chdir() will do.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  3. Received thanks from:

    muddyfox470 (15-01-2009)

  4. #3
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    Quote Originally Posted by aidanjt View Post
    That should do it. Although I would rather modify anything.exe to accept an optional directory argument than doing what is essentially 'shell scripting' in C, but if you don't have access to the source code for that, then chdir() will do.
    thank you very much.


    I'll have a look at it and see how the initial mini program turns out. I am sure I will be posting more questions in the near future
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  5. #4
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C Programming Help

    No problem, and good luck.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  6. Received thanks from:

    muddyfox470 (15-01-2009)

  7. #5
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: C Programming Help

    if you can't modify the orignal code, why do it in C?

    There are many other languages which make playing with the std i/o a lot easier, if your encapsulating programs, its a better way.

    Plus how are you launching the EXE, you should be able to set the 'working' folder that way, rather than moving the directory of the parent process.
    throw new ArgumentException (String, String, Exception)

  8. Received thanks from:

    muddyfox470 (15-01-2009)

  9. #6
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    I'm going to have a crack at it in a second, I wasn't really wanting to change the directory as i can just move the .exe into the same folder. So I'm guessing the chdir() prob isn't the code I'm looking for.
    I just wanted to post this up before I went off to hockey to see if any replies got here before i got back - so I'mglad people are looking and helping out

    The reason for C is that it is where most of my knowledge and experience is (taught as part of our engineering course), and I think the supervisor would prefer me to use it as he is very pro-c :s

    The question was regarding launching the .exe as i wanted to be able to launch it with the string-name (of the files required to run the simulation) automatically as part of the optimisation process.

    Is there a more logical way around this rather than using 3 different executables? (one control - one simulation (no source code) - one optimisation (which I am going to tailor [after finding a decent bit of code to mess with]) )
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  10. #7
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    Code:
    int main (int argc, char *argv[])
    {
        char simfile [50];
                 
             printf("Please type in the name of the file you wish to run with simulation:\n");
             scanf("%s",&simfile);
             system("pause");
             system("anything.exe", simfile);
             system("Pause");
    }
    Hello again. This is a little bit of the file I've created on the side, and it will run the program, but it is essential that in line system("anything.exe", simfile); it opens the file along with the file requested by the user, ive tried system("anything.exe hexus"); and it runs fine, but putting system("anything.exe simfile"); does not (as the file name is not correct) - so how do i get it to call that name in the address?

    i.e. so it would read like "anything.exe hexus" (hexus being the file), i cant seem to use the file name read in by the program, ID= "simfile"

    merci
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  11. #8
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,276
    Thanks
    292
    Thanked
    837 times in 473 posts

    Re: C Programming Help

    Does system() accept multiple arguments, or just one string? If the latter, you'll have to strcat() the supplied argument to the end of your command string.
    PHP Code:
    $s = new signature();
    $s->sarcasm()->intellect()->font('Courier New')->display(); 

  12. Received thanks from:

    muddyfox470 (15-01-2009)

  13. #9
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    Quote Originally Posted by Steve View Post
    Does system() accept multiple arguments, or just one string? If the latter, you'll have to strcat() the supplied argument to the end of your command string.
    Sorry for taking so long to get back to you, but along with a lit review that needs to be started, and losing my memory, to which I thought was a decent memory stick (Cruzer Titanium) I have had to start recovering data.

    But to report on my progress, and thanks to steve ( ) I've got the following and it works

    PHP Code:
    int main (int argcchar *argv[])
    {
        
    char simfile [50];
                 
             
    printf("Please type in the name of the file you wish to run with simulation:\n");
             
    scanf("%s",&simfile);
                
    char str[80];
                
    strcpy (str,"anything.exe ");
                
    strcat (str,simfile);
                
    puts (str);
                
    //system("pause");
                //return 0;
             
    system("pause");
             
    system(str);
             
    system("Pause");

    So thank you very much I'm sure there will be more to come
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  14. #10
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    Ok boys and girls...

    I have another question. I have an input file with a lot of data in it, I am trying to search the input file for a character

    The file goes something like this:

    C information and comments
    C information and comments
    C information and comments
    C information and comments
    C headings
    | xx xx xx xx xx xx xx
    C
    C ...

    Where xx represents a number that I want to read in.

    The | is the first occurence in the file and I want to read in the 7 bits of data to my array.

    How do I search for the | character and the &#37;f / %d the numbers following that, until it reaches the end of the line then closes the file (and I will then have an array of 7 numbers that I can use throughout the program).

    Thank you

    [If you require more info or more description let me know]
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  15. #11
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C Programming Help

    Use the "^|" regular expression string with the POSIX regex functions, see here for more on the functions:
    http://www.tin.org/bin/man.cgi?section=3&topic=regex

    It might need a bit of fiddling, but it should get you on the right track.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  16. Received thanks from:

    muddyfox470 (16-01-2009)

  17. #12
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    Thank you very much.

    Bizarrely I stumbled across something similar (someone wanting to separate#artist#title#xxx but it didn't give enough info in laymans terms ), but I couldn't get it to work with my file, but hopefully that site should help me a lot more.

    You won't believe how much time I spent (wasted) going around in circles and trying to get stuff to work. C-Programming is extremely frustrating, especially for someone with basic knowledge like mine.
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  18. #13
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    aidanjt (and others obv )

    because the regex library was hard to control and I seemed to only be able to get hold of the older library etc it kept on throwing up errors, so having spent a good few hours on the problem I went to see my supervisor, who commented I could read in the buffer to a string (which then i decided should be an array, so more lines could be stored).

    This is what I've got so far

    Code:
    FILE *fp;                   
                  int m;
                  m=0;
    
    fp = fopen(fileload, "r");
    
    ...
    
    while( fgets(buffer, sizeof(buffer), fp) != NULL )
                          
                         fputs(buffer, stdout);
                                              
                         if (buffer[0]='|')
                          {
                          sprintf(buffer,data);
                          data1[m]=data;
                          m++;
                          }
                         else
                         fputs(buffer, stdout);
                         fclose(fp);
                  }
    fputs(buffer, stdout); just puts it out onto the screen
    but i was hoping my if statement if (buffer[0]='|') would find the first character and read that line (until it encounters \n ) and save it to the string, which I then put into an array.

    Any help or pointers [no geeky pun intended]

    Cheers
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  19. #14
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts

    Re: C Programming Help

    Bit of a mix up between equality and assignment there.
    To err is human. To really foul things up ... you need a computer.

  20. #15
    Senior Trouble Maker muddyfox470's Avatar
    Join Date
    Jul 2004
    Location
    moving to Suffolk
    Posts
    3,103
    Thanks
    104
    Thanked
    46 times in 39 posts
    • muddyfox470's system
      • Motherboard:
      • Abit I-N73HD
      • CPU:
      • E4500
      • Memory:
      • 4Gb PC6400 Corsair ?
      • Storage:
      • 2 x Seagate 7200.12 500Gb and 1 x Hitachi 7k1000.b 750gb
      • Graphics card(s):
      • Powercolor 4850
      • PSU:
      • Corsair HX520W
      • Case:
      • Silverstone SG-01e
      • Monitor(s):
      • Fujitsu D22W-1
      • Internet:
      • BT Home

    Re: C Programming Help

    Quote Originally Posted by yamangman View Post
    Bit of a mix up between equality and assignment there.
    in the if statement?

    i was trying to think of what else i could for it to find the initial character in the file for each line, without the if statement the file reads fine and the information is printed to the screen, but it is the select lines that i want to save to a string and then an array.
    Mac fancier > white macbook base spec .................. CS: muddyfirebang

  21. #16
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C Programming Help

    Quote Originally Posted by muddyfox470 View Post
    Code:
    FILE *fp;                   
                  int m;
                  m=0;
    
    fp = fopen(fileload, "r");
    
    ...
    
    while( fgets(buffer, sizeof(buffer), fp) != NULL )
                          
                         fputs(buffer, stdout);
                                              
                         if (buffer[0]='|')
                          {
                          sprintf(buffer,data);
                          data1[m]=data;
                          m++;
                          }
                         else
                         fputs(buffer, stdout);
                         fclose(fp);
                  }
    Need to see more of that snippet, particularly how you declared the string variables being used. That looks a little odd. Particularly the buffer comparison in the if statement. Also, the sizeof() function will only return the size of the pointer object, that is the size of a char (usually 1 byte), instead of the actual size of the buffer you want. Although the first byte of each line is what we're interested in, you should do that after loading the line into the buffer and make it clear in the code what you're trying to accomplish.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PIC programming
    By explicitlyrics in forum Software
    Replies: 12
    Last Post: 19-10-2008, 02:21 PM
  2. Programming Magazine
    By Matt1eD in forum Software
    Replies: 4
    Last Post: 04-03-2005, 09:07 PM
  3. Help a Programming Novice
    By Xaneden in forum Software
    Replies: 37
    Last Post: 15-02-2005, 08:02 PM
  4. A few [geeky] programming lang jokes
    By Spud1 in forum Software
    Replies: 3
    Last Post: 02-02-2005, 05:19 PM
  5. C Programming Resources
    By Anders in forum Software
    Replies: 2
    Last Post: 08-05-2004, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •