0

I would like to print the words to the screen, which length's is shorter than the average length of the all words which are in a text file.

My text file : in1feladat.txt

words are here:

negy
ötötö
hathat
hetheth
nyolcnyo

so the average length is 6 (4+5+6+7+8 / 5 = 6) so the program should write to the screen : negy ötötö

my txt is located next to the main.c so its in the project folder. C:\Users\lszima\Desktop\proggg\fannikahelp\in1feladat.txt

codeblocks returns with the following error: Process terminated with status -1073741819 (0 minute(s), 1 second(s))

debug : Program received signal SIGSEGV, Segmentation fault. 0x4013dd line 16 which is the while(!feof(f1)

Can somebody help me out, what did i do wrong?

Thank you very much.

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

 int main()
 {
     FILE *f1=fopen("c:\users\lszima\desktop\proggg\fannikahelp\in1feladat.txt","r");
     double atlag=0;
     int sum=0;
     int t[10];
     int i=0;
     char szo[100];
     while(!feof(f1)){
         // szavak hossza belerakása a tömbbe
         //fscanf(f1,"%s",szo);
         fgets(szo,100,f1);
         t[i]=strlen(szo);
         i++;
     }

     int j=0;
     for (j=0;j<i;j++)
          sum+=t[j];
     atlag=(double)sum/(i+1);

     printf("atlag= %lf",atlag);
     fclose(f1);

     FILE *f1=fopen("c:\users\lszima\desktop\proggg\fannikahelp\in1feladat.txt","r");

     char szavak[100];
     while(!feof(f2)){
          //fscanf(f2,"%s",szavak);
          fgets(szavak,100,f2);
          if(strlen(szavak)<atlag)
              printf("%s ",szavak);
     }
     return 0;
}
Szima L.
  • 23
  • 7
  • 3
    For starters, [`while(!feof())`](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) is always wrong. – unwind May 24 '17 at 14:27
  • What is the **exact** contents of the text file being used? Paste it into your question using the code formatting option. Now run the program in the debugger. **When** does it fail? Get a backtrace from that point, and put that in your question too. Also, please indent your code properly, and put spaces around operators. Right now it's basically a solid, unreadable mass. Apart from the extra blank lines in some places for no apparent reason. – underscore_d May 24 '17 at 14:28
  • 2
    Check return value of `fopen`. – BLUEPIXY May 24 '17 at 14:31
  • you have to check that `i` is below 10, why not compute sum directly into the while loop. You have to open only once the file and rewind it – Ôrel May 24 '17 at 14:33
  • i edited my sourcecode like this: FILE *f1=fopen("in1feladat.txt","r"); if(f1){ etc... it gives 0 so basically the fopen is wrong... – Szima L. May 24 '17 at 14:34
  • `my txt is located next to the main.c` - and wherever `main.c` is is not guaranteed to be where the compiled executable runs from, possibly explaining why it can't open the file. You need to ensure that the file gets put in the same folder as the executable, or in your `PATH`, or instead `fopen()` it using an absolute path. – underscore_d May 24 '17 at 14:35
  • 1
    Better yet, pass the path to the file as a command line argument. – William Pursell May 24 '17 at 14:40
  • FILE *f1=fopen("C:\Users\lszima\Desktop\proggg\fannikahelp\in1feladat.txt","r"); i have put the aboslute path it gives error: incomplete universal character name\U warning: universal chacater names are only valid in C++ – Szima L. May 24 '17 at 14:41
  • Quoted code only has `while(!feof(f1)){` near line 16, not `while(feof(f1)`. – Yunnosch May 24 '17 at 14:45
  • corrected.sorry it was a typo – Szima L. May 24 '17 at 14:49
  • the function: `fopen()` returns a pointer, not an integer. so it never returns 0, but if it failed, it will return NULL. When it returns NULL, then call `perror( "fopen failed" );` this results in output to `stderr` of "fopen failed" followed by the error message supplied by the system. The error message tells why the system thinks the call to `fopen()` failed. – user3629249 May 24 '17 at 16:24
  • did you ever actually compile the posted code? When compiling, always enable all the warnings. Then, amongst other warnings would be: a warning about the second declaration of the variable `f1` and the undefined variable `f2` – user3629249 May 24 '17 at 16:27
  • in the path(s) to the target input file, the \ characters must be doubled. I.E. \\ – user3629249 May 24 '17 at 16:35
  • the posted code contains several 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 10, 100. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using a `enum` statement or `#define` statements to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code. Note: in general, the max characters (second parameter of `fgets()` ) should be something similar to `sizeof(szavak)` rather than a hard coded number – user3629249 May 24 '17 at 16:40
  • regarding: `double atlag=0;` the '0' literal is an integer, but it is being assigned to a double. (the implicit conversion feature may save you, but you should not depend on such conversions. Suggest: `double atlag=0.0;` Similar considerations exist for: `t[i]=strlen(szo);` where a `size_t` is being assigned to an `int` – user3629249 May 24 '17 at 16:45
  • the call to `fgets()` inputs everything on the line, including the trailing newline sequence. So after every call to `fgets()` there needs to be something like: `char *newline = NULL; if( NULL != (newline = strchr( szo, '\n' ) ) ) { *newline = '\0'; }` – user3629249 May 24 '17 at 17:15

1 Answers1

0

I have found an another - simplier solution, thank you very much for your help all.

cheers.

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

int main(){

char szo[50];
 int db=0, sum=0;
FILE * fin=fopen("in1feladat.txt","r");

while(fscanf(fin,"%s",szo)!=EOF)
{sum+=strlen(szo);
db++;

}
double avg=sum*1.0/db;

fclose(fin);
fin=fopen("in1feladat.txt","r");
while(fscanf(fin,"%s",szo)!=EOF)
{if(strlen(szo)>avg)
printf("%s\n",szo);

}


fclose(fin);

return 0;
 }
Szima L.
  • 23
  • 7
  • strongly suggest: `instead of the first call to `fclose( f1 )`, replace with `rewind(f1)` the remove the second call to `fopen()` – user3629249 May 24 '17 at 16:59
  • strongly suggest: follow the axiom: *only one statement per line and (at most) one variable declaration per statement. This kind of thing: `{sum+=strlen(szo);` makes the code much more difficult to read/understand/debug – user3629249 May 24 '17 at 17:01
  • for ease of readabiliy and understanding: 1) consistently indent the code. indent after every opening brace '{'. unindent before every closing brace '}'. Suggest each indent level be 4 spaces. 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line. – user3629249 May 24 '17 at 17:04
  • this answer still fails to check (!=NULL) the returned value from `fopen()` to assure the operation was successful. – user3629249 May 24 '17 at 17:05
  • leaving out common horizontal spacing is one of the ways to obscure C code. That is a poor programming practice. The code will execute exactly the same if it is written to be very readable. At a minimum insert a space inside parens, after `;` (semicolons) and after `,` (commas) – user3629249 May 24 '17 at 17:08
  • `if(strlen(szo)>avg)` --> `if(strlen(szo) – BLUEPIXY May 24 '17 at 22:43
  • @user3629249, Strongly suggest, use a temporary file to store words. This is a two pass algorithm, and sometimes (as working with input redirected from a pipe, for example) that you have no access to `rewind(f1)` to make the second pass. – Luis Colorado May 26 '17 at 07:42
  • @szimaL, if you want to illustrate something, and you take the time to write it as a response, please, read the SO documentation about how to format the source code and edit your answer to make it readable at all. Thanks. – Luis Colorado May 26 '17 at 07:44