Thread: plss help

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    plss help

    Im making a program to my school project, but im stuck now so i hope u can help

    we got a file which is suppose to strem.
    its printing (name /t number /t grade)

    we cant make any changes to that program.

    now i have to make a new program which reads this execute file and get the name, number and grade and put dem in different variables like char and int

    pls helppp

    thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's \t, not /t.

    Do you know how to open a file? Read from one?

    Maybe there's something in the tutorials about that kind of thing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's one about "C file I/O": http://www.cprogramming.com/tutorial/cfileio.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    hehe yeah \t

    well a already combiled program.... i thought i wasnt possible to read from a file which is already combiled

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean, open an executable? And "read" it? Good luck . . . . You can't really do that. Use a regular file instead.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    oh well thanksss... there must be a way.. all though it is a school project given by the school

    when i start the file it just start streaming eg. james 0000000 1

    i was told i could open the file through my program and capture the text which is being print our i guess

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can use fscanf() to read from a file the same way you would use scanf() to read from stdin (the keyboard).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    oh thank u very much... ill try to read a little about fscanf()


  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The other functions you will need are fopen() and fclose(), which open and close files (see my link above).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    what am i doing wrong ????
    Code:
    #include <stdio.h>
    
    #include <unistd.h>
    
    int main()
    {
    char output[1024];
    FILE *fp;
    fp=fopen("./riget", "r");
    
    
      fscanf (fp, "%c", &output);
    fclose (fp);
    
      printf ("%c", output);
    
    
    exit(0);
    }
    when i just start the file (./riget) it start to print this
    0408863864 4 F
    2307192198 4 D
    2107850859 1 D
    0702831830 3 B
    2808651656 1 C
    2810851859 2 C
    1610993997 4 F


    i need the numbers, the number in the secound row, and the letter in three different varibles

    and thanks.. im totally new in c programming

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Use fgets() to read a whole line.

    Code:
    while ( fgets( output, sizeof output, fp ) != NULL ) {
      /* do something with output */
    }

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or fgetc() to read a single character.
    Code:
    int c;
    while((c = fgetc(fp)) != EOF) {
        /* do something with character */
    }
    0408863864 4 F
    2307192198 4 D
    2107850859 1 D
    0702831830 3 B
    2808651656 1 C
    2810851859 2 C
    1610993997 4 F


    i need the numbers, the number in the secound row, and the letter in three different varibles
    Use fgets() with sscanf(), or fscanf().
    Code:
    fscanf(fp, "%*i %i %c", &thenumber, &thecharacter);
    The %*i tells [f]scanf to read a number, but not to store it in anything.

    Is that what you wanted?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    yes it is more than clear from that code that you're new!! there's a few things wrong there that i can see just straight off - why are you using unistd.h - not using anything thats not in stdio...

    ridget doent have a file extension, so thats never good.

    you should always check to see that a file opened correctly, like this:

    Code:
    if ((fp=fopen("./riget.txt", "r")) == NULL)
    {
           printf ("Error opening file");
           return 1;   /*the 1 is only to indicate an error - its good practice though to use a different number than 0 when there is an error*/
    }
    fscanf (fp, "%c", &output); - this will only read a single character change %c to %s, and you dont need the & since output is an array. change the %c to %s in your printf too.

    note the use of return - you shouldnt use exit (0) to quit a program, always use return 0; thats the standard (0 indicates success).

    also, fscanf will stop at the spaces, so you should probably use fgets instead.

    i havent given you all the answers you need - research them yourself so you will learn for yourself, i've just pointed you in the direction of your problems.

    one final note - in the words of Salem - the resident Grand Master: "Your indentation sucks". it should really be more even.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    thank u guys... all try it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PLss HeLpppppp
    By MoraL in forum C Programming
    Replies: 5
    Last Post: 05-30-2008, 09:23 AM
  2. Can someone Help me?
    By Mitchell in forum Windows Programming
    Replies: 14
    Last Post: 12-13-2001, 11:25 AM