0

There is a global array b[], that needs to be used in the main() and some other functions as well. Now the size of this array needs to be decided according to the input file size. The structure would be something like:

#include <stdio.h>
#include <conio.h>

char b[];

main() {

    FILE *f;

    f=fopen("Text.txt", "rb);

    if(file_size = 200)
       b[20];
    else if(file_size>200)       // I want to do something like this
       b[50];


    //Accessing b[] in the main

}

display() {

    //Accessing b[] in display

}

How should I do it by calculating the size of file. As the array b[] is being used in main() as well as in display(), it has to be global. However, I don't know how to calculate the size of the file and allocate it to the buffer before main().

user3213918
  • 333
  • 1
  • 6
  • 11

2 Answers2

2

Here's some code adapted from an opengl programming wikibook:

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

long file_size(File* file)
{
  if(input == NULL) return -1;

  long pos = ftell(input);

  if(fseek(input, 0, SEEK_END) == -1) return -1;
  long size = ftell(input);
  if(size == -1) return -1;
  if(fseek(input, pos, SEEK_SET) == -1) return -1;

  return size;
}

int display(char* b) 
{
  //Accessing b[] in display
}

int main() 
{

  FILE *f;

  f=fopen("Text.txt", "rb");

  long size = file_size(f); //returns -1 on error

  if(size == 200)
     b = malloc(20 * sizeof(char)); //replace char if you change b's type
     b = malloc(50 * sizeof(char));

  //b[0] = something, etc //access b[] 
  //display(b); //pass b to display function

  free(b);  
}
user3125280
  • 2,779
  • 1
  • 14
  • 23
  • Is this what you meant: char *b=0; main() { FILE *f; f=fopen("Text.txt", "rb); b=file_read("G:\\workspaceC\\small4.txt"); //Accessing b[] in the main } Its giving an error "conflicting types for 'file_read'" – user3213918 Jan 23 '14 at 04:41
  • @user3213918 does it work now? it is possible yuo didn't #include – user3125280 Jan 23 '14 at 06:04
  • Still not working. I have copy pasted the code you gave above, after the main(). I have also tried adding the stdlib.h Error "conflicting types for 'file_read' – user3213918 Jan 23 '14 at 06:08
  • it compiles fine for me on ideone without conio.h, so possibly the name file_read is being used in that header as well. try renaming it. – user3125280 Jan 23 '14 at 06:11
  • Ok thanks. I followed the example you gave and now its giving no error... but can't I do like. after getting the size of the file I decide my own array size for example: if(b==2375) //2375 is the size of file { b[60]; } else b[30]; – user3213918 Jan 23 '14 at 23:34
  • Oh ok. The code I gave you reads a file into memory. You should edit your question to say more precisely what you are trying to do, and I will edit my answer. – user3125280 Jan 24 '14 at 01:10
  • I have edited my question a bit. Please let me how to do that. Thanks – user3213918 Jan 24 '14 at 08:00
1

Succinctly, you can't do what you are seeking to do, namely change the size of a global array after the program starts executing.

Consequently, you will need to use dynamic memory allocation instead. Change the declaration to:

static char *b = 0;

The static means it can only be accessed by the functions in this file, but since you don't show any other files, that won't matter. If you do need to access the variable in other files, you'll need a header to declare the variable and you'll remove the keyword static.

The use of char *b in place of char b[] means you can allocate the space you need with malloc() or one of its friends (using #include <stdlib.h> to declare them), and then use free() to release the memory when you're done. You will also need to tell other parts of the software how much memory is allocated, so there'll be another variable such as:

static size_t b_size = 0;

which you will set to the size when you allocate it. After you've allocated the memory, you can use b just as if it was an array.

Don't forget to check that the allocation succeeded.

As to finding file size, you can either use a platform-specific function such as stat() on POSIX-based systems (Unix et al), or you can use fseek() and ftell(). These are reasonably portable, but are limited to handling files small enough that the size can be represented in a long. On 64-bit systems, that's not a problem; for 32-bit systems, it can be a problem if you might need to deal with multi-gigabyte files (but of course allocating multiple gigabytes of memory is also fraught with 32-bit systems).

Finally, for now, the name b is not a very good name for a global variable. It is generally a good idea to use more meaningful names, especially for global variables. (I use names like b as local variables in a function; I'd not use it as a global in any plausible circumstances.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278