Thread: File Server Help

  1. #121
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Actually wait... if it's sorted, you couldn't, because it's a list. But with a sorted array you could.

  2. #122
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    How do you say I can do it with an array? cause I can't have a fixed array if I want to support multiple connections or can I?

  3. #123
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why do you assume you can't do this with a fixed array? You could also dynamically resize an array if necessary. I recommend against the linked lists for the reasons stated in my previous post. You aren't doing that many connections anyway. You aren't writing a server comparable to google's.

  4. #124
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Right. I'd just stick with an array and a loop to find the elements. Keep it simple.

  5. #125
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Can you give me an example of how can i resize the array each time I need to do it? Well.. I'm trying to do this program to communicate some offices and save all the files into a server.. I might need a big array..

  6. #126
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can use realloc(). With any sort of web application I typically allocate large pools anyway. I prefer to manage my own memory. That not withstanding, why not just make a 1MB buffer or something for your arrays? I doubt you will need more than 1MB (1048576 bytes).

    [edit]I don't know what it is with people new to programming. Memory is a luxury that we have to work with in this day and age. Nowadays if you need a 15MB buffer, its not so outlandish. Don't be afraid of allocating chunks of memory that are 10,000 * sizeof YourClass. In the grand scheme of things that usually ends up being like 40k of memory...[/edit]

  7. #127
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Mmm.. I've been reading about realloc(), but how can i change the size of an array using bytes size? and to assign a value to the new element of the array? can you give me a little example? I'll try using realloc() to learn as much as I can about c =)
    Last edited by lautarox; 09-17-2008 at 08:23 PM.

  8. #128
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    search the board.

  9. #129
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Mmm.. looking in the forums I saw an array of strucs, and I liked it more than a simple array, I want to have more info about each file transfer..
    Is this ok?
    Code:
    struct _fileinfo {
    FILE *file;
    int parts;
    int totalparts;
    };
    
    int totalconnections;
    
    void memoryfunct() {
    struct _fileinfo *arr;
    realloc(arr, (totalconnections * sizeof(*arr)));
    arr[i].file="fopen(filepath, "ab");
    ..
    ..
    ..
    }
    Last edited by lautarox; 09-18-2008 at 11:24 AM.

  10. #130
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  11. #131
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Is it ok like this?
    Well.. I'm still not understanding how can I create or resize an array using realloc(), I mean, I create a pointer, or I give it a bigger size taking in consideration the size of my struct.. how the program knows that I'm creating an array?
    Code:
    struct _fileinfo {
    	FILE *file;
    	int parts;
    	int totalparts;
    };
    
    int totalconnections;
    
    void memoryfunct() {
    	struct _fileinfo *arr;
    	realloc(arr, (totalconnections * sizeof(*arr)));
    	arr[i].file="fopen(filepath, "ab");
    ..
    ..
    }

  12. #132
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you used realloc. What else is it going to be? (Answer: a single object, but if you try hard enough, you can think of that as a 1-element "array". I'm putting "array" in quotes because you are not creating a thing of array type, but you are creating a thing that you can use index[notation] with.)

  13. #133
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >Is it ok like this?

    no. read the link.

  14. #134
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I need a little help understanding the code..
    Code:
    #define MAXLINE 100
    
    char line[MAXLINE];
    int *ip;
    int nalloc, nitems;
    
    nalloc = 100; -> //100..
    ip = malloc(nalloc * sizeof(int));/* initial allocation */ -> // Why is is multiplying the size with 100? is it the same as "someinteger[100]" ?
    if(ip == NULL)
    	{
    	printf("out of memory\n");
    	exit(1);
    	}
    And about this..
    Code:
    nitems = 0;
    
    while(getline(line, MAXLINE) != EOF)
    	{
    	if(nitems >= nalloc)
    		{				/* increase allocation */
    		int *newp;
    		nalloc += 100;
    		newp = realloc(ip, nalloc * sizeof(int));  // -> Is it adding 200 more integers to the arr?
    		if(newp == NULL)
    			{
    			printf("out of memory\n");
    			exit(1);
    			}
    		ip = newp;
    		}
    
    	ip[nitems++] = atoi(line); -> // what does ip[nitems++] is supposed to do?
    	}

  15. #135
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >ip = malloc(nalloc * sizeof(int));/* initial allocation */ -> // Why is is multiplying the size with 100? is it the same as "someinteger[100]" ?

    An array's elements are all next to each other in order, so yes.

    >nalloc += 100;
    >newp = realloc(ip, nalloc * sizeof(int)); // -> Is it adding 200 more integers to the arr?

    No, it adds 100, for a total of 200.

    >ip[nitems++] = atoi(line); -> // what does ip[nitems++] is supposed to do?

    ip[nitems++] = atoi(line);

    is the same as

    ip[nitems] = atoi(line); nitems = nitems + 1;
    Last edited by robwhit; 09-19-2008 at 07:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM