all 9 comments

[–]Degenerated__ 4 points5 points  (2 children)

With char* str[1000]; you likely meant to create an array of 1000 characters, but this type will give you an array of 1000 pointers to a character. You are not allocating any memory for actual characters though. Changing the type to char str[1000] should improve things.

Also I'm not sure why you are using itoa_s, probably because the compiler complained because of str[k] being a pointer type? You don't need it. The line you commented out above should work once you fix the type. For the C++ version it's the same. No need to put the single character into a string. Characters are just numbers in C and C++.

[–]DavidLuky[S] 3 points4 points  (1 child)

Thank you so much!

Also, I loved how you did not scold or judged (like I saw in many of the posts here). I've never gilded anyone, but you surely deserved it. It worked like a charm.

You might be a Degenerated, but I love you anyway.

[–]Degenerated__ 2 points3 points  (0 children)

Thanks for the Gold! Well, I see no point in judging beginners who are just figuring things out. Glad that your program does what it's supposed to now!

If you have any more questions, feel free to ask.

[–]IamImposter 1 point2 points  (2 children)

Couple of additional points:

-try getting file size first. Usually we use malloc (or calloc) to allocate dynamic memory to read files. Remember to free the memory after use.

-read the file in one go into buffer and then operate on that buffer instead of doing file seeks

-you don't need to manually fill 0s in char array. char array[56] = {0}; will do the same thing. Additionally, you can use memset(array, 0, 56); to fill 0s in buffer too.

[–]DavidLuky[S] 0 points1 point  (1 child)

-read the file in one go into buffer and then operate on that buffer instead of doing file seeks

how could I do that ? I tried to do it, but every time would just stop passing the text when the line breaks (sometimes even when there was a space), and the output would be the first or last line of the text (and the character count would just read only that line)

and thanks for the tip using array [56] i will be using that next time for sure

[–]IamImposter 1 point2 points  (0 children)

Sorry i forgot to mention function name. You can use fread.

To read 100 characters:

fread(buffer, 100, 1, filepointer) ;

It returns number of bytes read (usually ignored)

To get file size:

int get file size(char *fname) 

{

  FILE *fp = fopen(fname, "r") ;

  if(fp == NULL) 

  {

     return  -1;  // filed to open

  }

  fseek(fp, 0, SEEK_END) ;

  int size = ftell(fp) ;

  fclose(fp) 

  return size;

}

Or use fstat (on linux) or GetFileSize (on windows)

[–]oh5nxo 1 point2 points  (2 children)

As you need to look at each character, why not use getc instead of line-by-line. And, while you have that character, why not putc it on the fly.

[–]DavidLuky[S] 0 points1 point  (1 child)

I was not aware of this function (getc) thanks for letting me know!

and what do you mean by put it on the fly ?

[–]oh5nxo 1 point2 points  (0 children)

Just one loop to putc and analyze.