it gets compiled but it crushes: "programm has stopped working"
Please help me!!!

this is the programm:

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

int main(int argc, char *argv[])
{

 FILE *fp;

int i=0, n;
char allwords[100][40];

fp=fopen("fraoules.txt","r");
if ( (fp=fopen("fraoules.txt","r") )==NULL ){
         printf("To arxeio den mporese na anoixtei\n");
         }

 while(strcmp( allwords[i],"telos")!=0){
    fscanf(fp, "%s", allwords[i+1]);
       i++;}

       fclose(fp);

for(n=0; n<i; n++)
printf("%s", allwords[n]);
  system("PAUSE");  
  return 0;
}

I do not understand, you are doing string compare with uninitialized allwords array, before you read word inside the while, shouldn't you use for or do..while and check after reading?.

As well as pyTony's suggestion, in lines 12 and 13 you are trying to open the same file twice.

Your could restart with something like this template. You surely have to read the tutorial http://www.daniweb.com/software-development/c/tutorials/45806/user-input-strings-and-numbers-c for future, if you did not allready. Now you are not taking input from user, but surely you need that knowledge soon. Do not forget to reserve place also for null character terminating the string!

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

#define BUFFERSIZE 256  // Make sure lines will not be longer than this

char LineBuffer[BUFFERSIZE];

int main ()
{
    FILE* infile =  fopen("words.txt", "r");
    if(infile == NULL)
    {
        fprintf(stderr, "Failure to open file for reading!");
        return(EXIT_FAILURE);
    }

    while (fgets(LineBuffer, BUFFERSIZE, infile) != NULL)
    {
        // lines will be truncated if longer than BUFFERSIZE
        printf("%s", LineBuffer);   // now output to console
    }

    fclose(infile);

    return(EXIT_SUCCESS);
}

Edited 4 Years Ago by pyTony: fprintf to stderr

This article has been dead for over six months. Start a new discussion instead.