0

I want to read a text file, character by character, and then do something with the characters and something with the words. This is my implementation:

char c;
char* word="";
fp = fopen("text.txt","rt");
do
{
    c = (char)fgetc(fp);
    if(c == ' ' || c == '\n' || c == '\0' || c == '\t')
    {
        //wordfunction(word)
        word = ""; //Reset word
    }
    else
    {
        strcat(word, &c); //Keeps track of current word
    }
    //characterfunction(c);
}while(c != EOF);

fclose(fp);

However, when I try to run this my program instantly crashes. Is there a problem with setting word to ""? If so, what should I do instead?

user2154420
  • 442
  • 4
  • 17
  • `strcat(word, &c);` wrong. `""` is no space that can bind a string. `&c` is not C_String. also `c` might EOF mock. – BLUEPIXY Oct 21 '14 at 01:02
  • possible duplicate of [Reading char by char from an input file in C?](http://stackoverflow.com/questions/19029852/reading-char-by-char-from-an-input-file-in-c) – Ken Y-N Oct 21 '14 at 01:05
  • 1) It will prepare the area of sufficient size. E.g `char word[1024];` 2) use a dynamic string. – BLUEPIXY Oct 21 '14 at 01:07
  • What is a dynamic string? – user2154420 Oct 21 '14 at 01:08
  • Like to increase the size automatically according to the addition of the character. – BLUEPIXY Oct 21 '14 at 01:09
  • fgetc() returns an int, EOF is an int, so the definition of 'c' should be int – user3629249 Oct 21 '14 at 16:47
  • 'word' is defined as a ptr to char, and is not actually pointed to any valid memory. suggest change to: char *word = calloc( 100, 1 ); otherwise undefined behaviour is being used. – user3629249 Oct 21 '14 at 16:50
  • this line: word = ""; //Reset word should be: memset( word, 0x00, 100); (given the above comment about allocating 100 bytes for a word – user3629249 Oct 21 '14 at 16:52

2 Answers2

1

In your word variable initial assignment, you're pointing to a static string of length 0. When you try to write data into there, you'll overwrite something else and your program will brake. You need, instead, to reserve space to your words.

where you have

char* word="";

use instead

char word[100];

This will create a space of 100 chars for your word.

char c;
char word[100];
fp = fopen("text.txt","rt");
int index = 0;
do {
  c = (char)fgetc(fp);
  if(c == ' ' || c == '\n' || c == '\0' || c == '\t') {
    //wordfunction(word)
    word[0] = 0; //Reset word
    index = 0;
  } else {
    word[index++] = c;
    word[index] = 0;
    //strcat(word, &c); //Keeps track of current word
  }
  //characterfunction(c);
} while(c != EOF);

fclose(fp);
Joao
  • 619
  • 1
  • 5
  • 14
  • So then how do I reset the word each time? – user2154420 Oct 21 '14 at 01:09
  • The word end is defined by the '\0'. A way to do it is to set the initial char to '\0', as word[0] = 0; – Joao Oct 21 '14 at 01:11
  • That code worked, but code you explain in detail why word[0] = 0; "reset" the string the way I wanted it to? – user2154420 Oct 21 '14 at 01:13
  • 3
    C strings are terminated by the null character. That means that what defines the string end is the char 0. System functions to process strings look for that char to determine where the string ends. Said that, if you set the first char to 0, you're saying the string has no chars in it. – Joao Oct 21 '14 at 01:16
  • The `strcat()` is broken, so this change alone will not fix the code. – Ken Y-N Oct 21 '14 at 01:34
  • Instead of using strcat, keep track of the added chars using a counter and just place the added char in the right position – Joao Oct 21 '14 at 01:36
0
  1. word points to a constant area which is not allowed to be write by strcat, so your code dumped;
  2. word should have enough space to reserve chars, try hard-coded or realloc(word, size_t);

  3. this can be compiled with gcc -o:

int main(){ char c; char word[1000] = {0}; //enough space FILE* fp = fopen("text.txt","rt"); int index = 0; assert(0 != fp); do { c = (char)fgetc(fp); if(c == ' ' || c == '\n' || c == '\0' || c == '\t') { //wordfunction(word) index = 0; } else { word[index++] = c; } //characterfunction(c); }while(c != EOF); word[index] = 0; fclose(fp); return 0; }

Anthony Cooper
  • 465
  • 2
  • 15
  • Oops, sorry, my mistake - deleting my original comment. – Ken Y-N Oct 21 '14 at 01:36
  • @BLUEPIXY you just tell me "has many mistakes" and vote down and run away, why not list some "mistakes" ? – Anthony Cooper Oct 21 '14 at 01:39
  • [Why `c=(char)fgetc(fp);` is bad](http://stackoverflow.com/questions/11057259/fgetc-checking-eof). And \\ is not how you do comments. And `do{}while()` is a problem on an empty file. – Ken Y-N Oct 21 '14 at 01:39
  • 1
    1. `fgetc` comes from this question, ask @user2154420; 2. \\ is my mistake, sorry. 3. `do{}while()` has no problem on empty file, have you try it? – Anthony Cooper Oct 21 '14 at 01:41