Thread: About character.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    32

    About character.

    Is it possible to ask the program to read the line after a character?

    For example,

    1 1 1
    2 2 2
    3 3 3
    *****
    4 4 4
    5 5 5

    I want the program after it detects character '*'. Please advise.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes it's totally possible.

    Sit down and think about it for a while...
    plan out your code
    write it up
    test it.

    If you get stuck post your code here and I'm sure some of us will help...

    However; we don't hand out code except in rare cases. You don't learn anything if we just hand you the answers.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by unknownC View Post
    Is it possible to ask the program to read the line after a character?
    Of course, reject everything before and that character.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Quote Originally Posted by GReaper View Post
    Of course, reject everything before and that character.
    Code:
    if (c=='*')
    {
    c=c+4;
    getc(Matrix);
    }
    I am trying really hard. PLEASE PLEASE HELP!

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by unknownC View Post
    I am trying really hard.
    Yeah, so I see...

    Anyway, the idea to keep getting characters, and rejecting them, until you run onto the desired terminating symbol( in your case "*" ). Then all you'll have to do is read the rest in a buffer or something.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Quote Originally Posted by GReaper View Post
    Yeah, so I see...

    Anyway, the idea to keep getting characters, and rejecting them, until you run onto the desired terminating symbol( in your case "*" ). Then all you'll have to do is read the rest in a buffer or something.
    Are you referring to something like this?

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Boston, MA
    Posts
    97
    Well if you logically think about it for a second what do you actually want to do? If I am interpreting right you want to read the lines after the character then you are going to need a loop with a check. Read all the lines and throw them away until you hit the character, make the check true and read the following until EOF or whatever input method you are using.
    Last edited by omGeeK; 10-22-2011 at 08:16 PM.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Here's my try...

    Code:
    if(c=='*')
    {
    while (c=='*')
    {
    continue;
    if(c=='\n')
    break;
    }
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to be reading the file in there somplace...

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Code:
    if((c=fgetc(Matrix))=='*')
    {
    while (c=='*')
    {
    continue;
    if(c=='\n')
    break;
    }
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    How about
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, Matrix ) != NULL ) {
      if ( buff[0] == '*' ) {
        // do something, 
      } else {
        // do something with 3 numbers in buff
      }
    }
    Or as you were,
    Code:
    while ( (c=fgetc(Matrix)) != EOF ) {
      if ( c == '*' ) {
        // do something, 
      } else if ( c == '\n' ) {
        // do something, 
      } else {
        // do something, 
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    I still don't get it. Is it possible to instant messaging here? Or can someone send me an email at <<snipped>>? I don't want to post my homework script online.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So post something which looks like what you're trying to do.
    Say just the "read" and "print" a matrix code, and leave out all the matrix math code you've worked on.

    Nobody learns from a 1:1 email/chat session, and there's no way for you to really know whether the person helping you knows what they're talking about.
    Open forums are both more responsive (you're not waiting for one person), and self-correcting (any mistake is likely to get pounced on).

    Posting your genuine answer is seldom a problem, since you can always prove "I posted it first, therefore you must have copied mine" if it comes down to it.



    On the other hand, if you've just found your code on the web and are trying to get us to fix it for you by proxy, then we're likely to find out sooner or later, and that will be the end of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Code:
    do
    {
    
    while ((c=fgetc(Pattern))!='*')
    {
    //read 1st pattern [1 1 1;2 2 2;3 3 3]
    }
    
    //compare with Matrix file
    
    //Below is the part where I want the prog to read after '*' line so to the next pattern, which I am highly doing it wrongly.
    
    if ((c=fgetc(Patterns))=='*')
    {
    while((c=fgetc(Patterns))=='*')
    {
    	continue;
    }
    }
    }while((c=fgetc(Patterns))!=EOF);

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Put into words what you want to do. Then break those words down into the simplest steps you can. Then think about how to turn those simple steps into code.

    "I want to read lines from a file and when I run into the pattern that is just stars, I want to ... do whatever."

    I need to read a line from a file.
    I need to compare what I have read to what I want to find.
    I need to do something based on what I find.


    Keep working at breaking each step down into smaller steps until you can do the final step in words:
    Code:
    while not at the end of the file
        read one line and store it
        if what i read is something
            do something
        else
            do something else
    Now come back when you've done all that.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-05-2011, 08:21 PM
  2. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  3. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  4. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  5. gibrish when copying strings character by character
    By captain-cat in forum C Programming
    Replies: 2
    Last Post: 07-23-2004, 07:48 AM