0

I've been working with data written in .txt files and so had some problems.

Example of data: lettts ddo ttttthis
Example of answer: le3ts 2do 5this

I've tried to read 2 times information while second time ignoring first character and below the read function compare with for loop if(char1==char2) count++; But all it does is making all chars equal. Any suggestions how to compare chars properly?

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char ch,ch2,chvoid;
  int i,num=0,num2=0;
  FILE *fp;

  if (fp = fopen("file.txt", "r"))
  {
    while (ch != EOF)
    {
      ch = getc(fp);
      printf("%c", ch);
    }
    fclose(fp);
  }

  if (fp = fopen("file.txt", "r"))
  {
    chvoid = getc(fp);
    while (ch2 != EOF)
    {
      ch2 = getc(fp);
      printf("%c", ch2);
      num++;
    }
    fclose(fp);
  }
  for(i=1;i<num;i++){
    if(ch!=ch2){
      printf("test");
    }                 
    if(ch==ch2){
      num2++;     
      printf("%d ",num2);
    }       
    num2=0; 
  }
  return 0;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
user240179
  • 39
  • 8
  • Why can't you use arrays? Can we assume that this is an academic exercise? It looks like you're not far off, but you're comparing the incrementing ch2 against ch which is set to EOF by the first loop. – NickPoole Oct 25 '14 at 10:52
  • On closer inspection it looks like you're trying to iterate over the single char in the for loop as if it were an array, but actually both ch and ch2 are EOF. You need a different loop strategy: use the while not EOF and getc to iterate over the string, storing the previous char each time so you can compare against it. – NickPoole Oct 25 '14 at 11:02

4 Answers4

2
#include <stdio.h>

int main(void){
    FILE *fp;
    int ch, ch2, count;

    if(NULL==(fp = fopen("file.txt", "r"))){
        perror("file can not open.");
        return 1;
    }
    printf("Example of data: ");
    while((ch=fgetc(fp)) != EOF){
        putchar(ch);
    }
    putchar('\n');
    rewind(fp);

    printf("Example of answer: ");
    count = ch2 = 0;
    while(1){
        ch=fgetc(fp);
        if(ch2 != ch){
            if(count > 1)
                printf("%d", count);
            ch2 && putchar(ch2);//if(ch2)putchar(ch2);
            if(ch == EOF)
                break;
            count = 1;
            ch2 = ch;
        } else {
            ++count;
        }
    }
    fclose(fp);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Maybe something like this?

    while( data[i] != '\0' )
    {
        char c = data[i++];
        int counter = 1;

        while( c == data[i] )
        {
            counter++;
            i++;
        }

        if( counter - 1 )
            printf( "%d", counter );

        printf( "%c", c );
    }
stryku
  • 722
  • 5
  • 12
0

Ok so maybe this. I don't know you want data from file

#include <stdio.h>
#include <stdlib.h>

int main( )
{
    char ch, ch2, chvoid;
    int i, num = 0, num2 = 0;
    FILE *fp;

    if( fp = fopen( "file.txt", "r" ) )
    {
        while( ch != EOF )
        {
            ch = getc( fp );
            printf( "%c", ch );
        }
        fclose( fp );
    }

    if( fp = fopen( "file.txt", "r" ) )
    {
        int c = getc( fp );

        while( c != EOF )
        {
            int c2 = getc( fp );
            int counter = 1;

            while( c2 == c )
            {
                counter++;
                c2 = getc( fp );
            }

            if( counter - 1 )
                printf( "%d", counter );

            printf( "%c", c );

            c = c2;
        }

        fclose( fp );
    }


    return 0;
}
stryku
  • 722
  • 5
  • 12
  • 3
    `getc` returns an `int` for very good reasons (see http://stackoverflow.com/questions/7622699/what-is-the-ascii-value-of-eof-in-c), storing its return value in a char will lead to problems. – Mat Oct 25 '14 at 10:58
-1

Fflush solution

You need to do. This will clear the buffer for both standard out and standard in "fflush(null)"

Understanding the need for fflush() and problems associated with it

Community
  • 1
  • 1
  • 1
    What does this have to do with the question above? – Mat Oct 25 '14 at 10:49
  • Just a suggestion as in the past i often had problems when iterating through multiple characters. Si just want to pass ut on – Alex Force Oct 25 '14 at 10:51
  • 1
    Suggestions like this should go in comments. The answer section on this site is only for actually answering the question. You'll get the required reputation (50) to comment in no time if you post quality answers or questions here. In the mean time, please don't add comments or suggestions like these as answers. – Mat Oct 25 '14 at 10:54
  • OK i apologize ..will remove it all together – Alex Force Oct 25 '14 at 10:56