0

I am trying to make a function that reads a text file which contains data and assign it to a variable. However some lines start with $ which need to be ignored. For example:

$ Monday test results
10 12
$ Tuesday test results
4

This is what I have so far which just prints out:

10 12
4

The code that does this is:

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

void read_data(){
  FILE* f;

  if (f = fopen("testdata.txt", "r")) {
       char line[100];
       while (!feof(f)) {
             fgets(line, 100, f);
             if (line[0] == '$') {
                  continue;
             } else{
                  puts(line);
             }
      }
  } else {
            exit(1);
   }
fclose(f);
}
void main(){
read_data();
return 0;
}

I have tried fgetc and have googled extensively but am still stuck ;(

**Edits Added #include and main

What I am asking is how to assign like a = 10, b = 12, c = 4. Had troubles since using fgets is for lines. Tried fgetc but it would only ignore the actual $ sign not the whole line that the $ is on

  • In the original question I am asking how to assign it to a variable. The #include and main file is pretty standard I have included it in my edit if needed however it isnt really needed for this function logic – CoffeeDrinker101 Aug 27 '20 at 10:55
  • 1
    You should normally include an [MCVE](https://dba.stackexchange.com/help/minimal-reproducible-example) so we can just run your code readily. Can you explain more what your data is? Is this all - the 4 lines you're showing, or is there more, with variable number of entries etc.? – atru Aug 27 '20 at 11:02
  • 1
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Yunnosch Aug 27 '20 at 11:29
  • This is part of a much larger project so can't share the whole thing. The whole function is there though. Just looking to find how to match a variable to the individual characters of a text file – CoffeeDrinker101 Aug 27 '20 at 11:50
  • 1
    @Dominique We edited the title at the same time. :-) – RobertS supports Monica Cellio Aug 27 '20 at 12:28
  • Please read: [why while( !feof() ) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Aug 28 '20 at 05:01
  • regarding: `void main(){` Per the C standard, there are only two valid signatures for the `main()` function: They are: `int main( void )` and `int main( int argc, char *argv[] )` – user3629249 Aug 28 '20 at 05:03
  • regarding: `while (!feof(f)) {` and `fgets(line, 100, f);` Much better to use: `while( fgets( line, sizeof( line ), f ) {` – user3629249 Aug 28 '20 at 05:06
  • OT: for ease of readability and understanding by us humans (the compiler doesn't care) Please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces – user3629249 Aug 28 '20 at 05:10

1 Answers1

1

C string.h library function - strtok()

char *strtok(char *str, const char *delim)
  • str − The contents of this string are modified and broken into smaller strings (tokens).
  • delim − This is the C string containing the delimiters. These may vary from one call to another.

This function returns a pointer to the first token found in the string. A null pointer is returned if there are no tokens left to retrieve.

Copied from: https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

#include <string.h>
#include <stdio.h>

int main () {
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

Output:

This is 
  www.tutorialspoint.com 
  website
paladin
  • 765
  • 6
  • 13
  • How can this be assigned to variables though? I need to use the data from Monday and Tuesday in other functions. – CoffeeDrinker101 Aug 27 '20 at 11:56
  • Isn't `token` the variable you're looking for? – Dominique Aug 27 '20 at 12:28
  • Sorry if this is simple, just want to completely understand, you can make multiple 'tokens' for each variable eg token1 token 2 token 3?? – CoffeeDrinker101 Aug 27 '20 at 23:47
  • Imagine the `strtok()` as a scissor for strings. Look at this line: `token = strtok(str, s);`, the string named `str` is your long string, which you want to cut into several pieces, the string named `s` is the marking for your cut. The example above uses the following string as marking: `"-"`. So `strtok()` now cuts your string `str` at _first_ position which is identical to string `s`. The marking is removed (deleted) in this process from your original `str`. Now your original `str` is cut into 2 pieces. One piece is the string return value `token`, the other is the remaining shortened `str`. – paladin Aug 28 '20 at 03:24