-1

I am new to C and am getting very frustrated with learning this language. Currently I'm trying to write a program that reads in a program textfile, reads and prints all the string literals, and tokens each on separate line. I have most of it except for one snag. within the text file there is a line such as: (..text..). I need to be able to search, read and print all the text is inside the parentheses on it's own line. Here is an idea I have so far:

#define KEY 32
#define BUFFER_SIZE 500

FILE *fp, *fp2;

int main()
{
  char ch, buffer[BUFFER_SIZE], operators[] = "+-*%=", separators[] = "(){}[]<>,";
  char *pus;
  char source[200 + 1];
  int i, j = 0, k = 0;
  char *words = NULL, *word = NULL, c;
                                                                                                          
  fp = fopen("main.txt", "r");
  fp2 = fopen ("mynewfile.txt","w") ;

  
while ((ch = fgetc(fp)) != EOF)
{
  // pus[k++] = ch;
  if( ch == '(')
  {

    for ( k = 0;, k < 20, K++){

      buffer[k] = ch;
      buffer[k] = '\0';

    }
    printf("%s\n", buffer)
   }

....

The textfile is this:

#include <stdio.h>

int main(int argc, char **argv)
{
    for (int i = 0; i < argc; ++i)
    {
        printf("argv[%d]: %s\n", i, argv[i]);
    }
}

So far I've been able to read char by char and place it into a buffer. But this idea just isn't working, and I'm stumped. I've tried dabbling with strcopy(), ands strtok, but they all take char arrays. Any ideas would be appreciated thank you.

CGuy
  • 23
  • 2
  • In order to read line by line (that seems to be exactly what you need) you can use `fgets`, than you can use `strstr` to search a substring in the current line and print all the line if it is found. – Roberto Caboni Sep 15 '20 at 19:29
  • [`int c`!](https://stackoverflow.com/q/35356322/918959). – Antti Haapala -- Слава Україні Sep 15 '20 at 19:33
  • Noted. as posted below I have gotten further clarification on reading line by line yet I need the "for", and "(...)", to be on their own lines. Also noted that ch, will be changed to an int. Should be more specific, and explain. Thanks guys. – CGuy Sep 16 '20 at 00:18

1 Answers1

0

Most likely the best way would be to use fgets() with a file to read in each line as a string (char array) and then delimit that string. See the short example below:

char buffer[BUFFER_SIZE];
int current_line = 0;
//Continually read in lines until nothing is left...
while(fgets(buffer, BUFFER_SIZE - 1, fp) != NULL) 
{
  //Line from file is now in buffer. We can delimit it.
  char copy[BUFFER_SIZE];
  //Copy as strtok will overwrite a string.
  strcpy(copy, buffer);
  printf("Line: %d - %s", current_line, buffer); //Print the line.

  char * found = strtok(copy, separators); //Will delmit based on the separators. 
  while(found != NULL)
  {
    printf("%s", found);
    found = strtok(NULL, separators);
  }
  current_line++;

}

strtok will return a char pointer to where the first occurrence of a delimiter is. It will replace the delimiter with the null terminator, thereby making "new" string. We can pass NULL to strtok to tell it to continue where it left off. Using this, we can parse line by line from a file based on multiple delimiters. You could save these individual string or evaluate them further.

Lucas Streanga
  • 469
  • 2
  • 7