-2

My main problem is when i run the program it not work and a runnig time error is jumping on my screen. Can someone explain me whats the problem and help me? *argv[i] is the adress and ignore the fact i don't have any free for my mallocs.

int main(int argc, char** argv)
{
FILE* file = 0;
file = fopen(argv[1], "r");
int numOfLines = countLines(file), i = 0, ch = 0, j = 0, flag = 0;
char** content;
content = (char**)malloc(numOfLines * sizeof(char*));
int* charsInLine = (int*)malloc(numOfLines * sizeof(int));
countCharsInLine(file, charsInLine);
fseek(file, 0, SEEK_SET);
for (i = 0; i < numOfLines; i++)
{
    int lenOfFile = 0;
    fseek(file, 0, SEEK_END);
    lenOfFile = ftell(file);
    content[i] = (char*)malloc(charsInLine[i] * sizeof(char) + 1);
    content = fread(content, 1, lenOfFile, file);
}
for (i = 0; i < numOfLines; i++)
{
    printf("%d string = %s", i,content[i]);
}
fclose(file);
getchar();
return 0;
}
Mort
  • 3,379
  • 1
  • 25
  • 40
  • 2
    You aren't writing anything to the `j` index that you skip, and, you are not writing a string terminator. If you want to skip characters, you need another variable to index the target string, which is only incremented when the string is written to. Perhaps the second `ch = fgetc(file);` is in the wrong place too. Just read from the file in a loop, and store in the target array. – Weather Vane May 22 '18 at 18:04
  • 1
    Have you considered using `fgets`? – Weather Vane May 22 '18 at 18:06
  • 1
    For every malloc, there must be a free. For every fclose, there must be an fopen. Please post a Minimum Complete Verifiable Example. Look at your input, add extra printf statements to examine variables. Use a single step debugger if you can. – Pam May 22 '18 at 18:07
  • 2
    I think there may be a number of flaws here depending on the input file. Please post a [mcve]. But you never reset `flag` to `0` so if you encounter `'\n'` at the expected point it will never re-enter the inner loop. – Persixty May 22 '18 at 18:10
  • I have edited I'll be glad if you could check it up. @Pam – מקס גופמן May 22 '18 at 18:16
  • I have edited I'll be glad if you could check it up. @Persixty – מקס גופמן May 22 '18 at 18:20
  • 1., [casting malloc is bad](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) 2. The second `ch = fgetc(file);` - What happens when EOF is returned? 3. Check the return value from `fopen` – Ed Heal May 22 '18 at 18:27
  • @EdHeal those are basic stuff that i'll take care of. The main problem is with the initialize of the stirng. – מקס גופמן May 22 '18 at 18:31
  • Why not take care of it (or not write it in the first place). BTW - There is a flaw in your algorithm with the use of the second `ch=fgetc(file);` – Ed Heal May 22 '18 at 18:32
  • BTW - Where is the code for `countLines` – Ed Heal May 22 '18 at 18:33
  • I'm stuck with my question those are stuff that takes a minute to make. What is the flaw? – מקס גופמן May 22 '18 at 18:34
  • @EdHeal I can't upload to much code in stackoverflow but it returns the num of line. – מקס גופמן May 22 '18 at 18:35
  • Are you sure that it works correctly? Does `countCharsInLine` work correctly. If both are correct you could just use `fread` instead of the loop. – Ed Heal May 22 '18 at 18:36
  • I'm sure they are working right. I'll try to use fread. – מקס גופמן May 22 '18 at 18:39
  • Perhaps output there return values and check. Also do not forget the null character – Ed Heal May 22 '18 at 18:40
  • yeah I have made + 1 in the malloc. Hoply the fread idea will work .thanks ;) – מקס גופמן May 22 '18 at 18:42
  • @EdHeal I have edited my code – מקס גופמן May 22 '18 at 18:47
  • @EdHeal when I run my code i got running time error – מקס גופמן May 22 '18 at 18:49
  • Not a [mcve], sorry. Many, *many* small and not-so-small problems with the code, not focussing on a specific problem. – DevSolar May 22 '18 at 18:50
  • @DevSolar My main problem is when i run the program it not work and a runnig time error is jumping on my screen. – מקס גופמן May 22 '18 at 18:53
  • 3
    Yes, but you shrug off lots of good advice, telling us "that's not where the problem is", and we're staring at *lots* of issues with your code that a "correct" answer *would all have to address because you didn't*. Please check again what [mcve] means. Especially the "minimal". This is debugging 101. Also, ref. [Machete Debugging](https://rootdirectory.ddns.net/dokuwiki/doku.php?id=software:machetedebugging) if the MCVE text is too long. – DevSolar May 22 '18 at 18:55
  • @DevSolar - I agree - correcting those small mistakes goes a long way to getting a correct solution. Just ignoring them is not playing cricket – Ed Heal May 22 '18 at 19:09

1 Answers1

1

I am going to assume countLines and countCharsInLine works correctly

Here is the updated code with comments

int main(int argc, char** argv)
{
   FILE* file fopen(argv[1], "r");
   if (file == NULL) {
      // Output some error message
      return EXIT_FAILURE;
   }

   int numOfLines = countLines(file); // I assume this rewinds.

   char** content;
   content = malloc(numOfLines * sizeof(char*)); // Do not need a cast

   int* charsInLine = malloc(numOfLines * sizeof(int));

   countCharsInLine(file, charsInLine);

   rewind(file); // Easier to read the fseek

   for (int i = 0; i < numOfLines; i++)
   {
      content[i] = malloc(charsInLine[i] + 1); // * sizeof(char) - Do not need this as sizeof(char) is defined as 1
      // Reading one item of size charsInLine[i]
      if (fread(content[i], charsInLine[i], 1, file) != 1) {
         // Some error has occurred
         fclose(file);
         return EXIT_FAILURE; 
      }
      content[i][charsInLine[i]] = 0; // Add null character

      int ch = fgetc(file);
      if (ch != '\n' && ch != EOF) { // Should be reading either the new line at the end of the line, or EOF
         // Some error has occurred
         fclose(file);
         return EXIT_FAILURE; 
      }
   }
   fclose(file);
   // Should free up the stuff that is malloced - I leave that up to you
   return EXIT_SUCCESS;
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127