User Profile

Collapse

Profile Sidebar

Collapse
krreks
krreks
Last Activity: Nov 20 '08, 06:51 PM
Joined: Oct 11 '08
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • krreks
    replied to Reading from file without caring about \n
    in C
    I've opened the file with mode: rb, but I'm still not able to read it properly.

    Code:
    while(!feof(input)) {
    	if(!ferror(input)) {
    		if(fread(inputBuffer, 1, MSG_MAX_LEN-1, input)) {
    			printf("%d chars read \n", strlen(inputBuffer));
    			reads++;
    		}
    	}
    }
    I'm trying to output this:


    I know there a few thousand chars in the file, but the code...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Reading from file without caring about \n
    in C
    The fread works great for ascii/text files, but is no good for binary files (e.g. compiled c programs).

    Is there a way that will work for all files?
    See more | Go to post

    Leave a comment:


  • krreks
    started a topic Reading from file without caring about \n
    in C

    Reading from file without caring about \n

    I have text files and want to read 1024 characters at the time, no matter if its \ns or something else.
    See more | Go to post

  • krreks
    replied to "Problem" with navigating in dirs with chdir...
    in C
    Found the realpath function a little before i got the notification about your reply, but thanks a bunch anyway :)

    BTW: Is there a reference on which operating system the different functions/libs is available?...
    See more | Go to post

    Leave a comment:


  • krreks
    started a topic "Problem" with navigating in dirs with chdir...
    in C

    "Problem" with navigating in dirs with chdir...

    I'm developing a very simple file server and client at school and have the following case.

    The server maintains a current directory string and also holds a initial dir (which is a record of where the script was started from). The user is able to navigate in files and folders on the server, but should not be able to navigate outside the initDir.

    My first idea was to keep the current dir in a temporary variable, change...
    See more | Go to post

  • krreks
    replied to Passing char[][] as argument
    in C
    I ended up solving my problem with the following piece of code.

    Code:
    char * fillFileArr(char * buf, int num, int maxLen) {
    	char * arr = (char *)malloc(num*(maxLen+1));
    
    	int word=0, wordPos=0, bufferPos=0;
    	while(word<num && buf[bufferPos]!='\0') {
    		if(buf[bufferPos]=='\n') {
    			arr[(word*(maxLen+1))+wordPos] = '\0';
    			
    			word++;
    			wordPos=0;
    		} else
    ...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Passing char[][] as argument
    in C
    It was late last night but as far as I remember i got seg faults in all three cases. I'm compiling with

    Code:
    gcc -o server server.c helpers.o protocol.o -I. -DDEBUG -g -Wall
    ...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Passing char[][] as argument
    in C
    I tried both casting the malloc to (char **), (char *) and nothing - same result......
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Passing char[][] as argument
    in C
    Ok... I have made another atempt on the previously mentioned function - this time I hope it might work with some tweaking - different from the last code, which seemed impossible to implement :)

    What I want to do is:
    Take a pointer to a string and and some numbers giving the dimensions of the text (longest words and number of words) which is needed to allocate enogh memory for all the data. Then I will allocate memory, fill the...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Passing char[][] as argument
    in C
    Ok


    gcc/cc in Xcode for OSX Leopard


    I'll check it out and look to it if I have the time - sort of in a hurry on this project, and this particular piece is not that critical...

    Thanks for all the help - I'll look into this again when I have the time :)...
    See more | Go to post

    Leave a comment:


  • krreks
    started a topic Passing char[][] as argument
    in C

    Passing char[][] as argument

    I'm trying to pass this array

    Code:
    char files[num][maxLen+8];
    like this

    Code:
    if(fillFileArr(&(files[0][0]), &(buf[0]), num)==-1)
    	return -1;
    to this function

    Code:
    int fillFileArr(char ** files, char * buf, int num) {
    	int word=0, wordPos=0, bufferPos=0;
    	while(word<num && buf[bufferPos]!='\0') {
    		if(word==0 && wordPos==0 &&
    ...
    See more | Go to post

  • krreks
    replied to String manipulation question
    in C
    Hi.

    The sprintf function was what I was looking for, I found it myself and the problem got solved :)

    Thanks anyway!
    See more | Go to post

    Leave a comment:


  • krreks
    started a topic String manipulation question
    in C

    String manipulation question

    I need to concatenate a string with a dynamic part (don't know what the %d's and it's likes are called)... And exactly the fact that I don't know the name of what I'm trying to do is what makes so hard to google it :)

    What I wan't to look something like this in my head:

    Code:
    strcat(ret, "! [%d] %s", i, buf);
    The code above won't work, but what will? I don't wan't to do a dozen of strcats...
    See more | Go to post

  • krreks
    replied to Defining struct in headers
    in C
    Works like a charm :) Thanks!
    See more | Go to post

    Leave a comment:


  • krreks
    started a topic Defining struct in headers
    in C

    Defining struct in headers

    I'm experiencing some (beginner) problems with my header files...

    I'm writing an message application and want to split my application into multiple files. So far, everything related to bitwise operations is in one, another one contains various other handy functions.

    Now I want to make a file with the phone and message related functions. In this file I would like to define the structs as well, but I'm facing the following...
    See more | Go to post

  • krreks
    replied to Char pointers in C
    in C
    That sounds right. I've really tried and failed a lot the last few days, but it's starting to pay off :)

    Thanks a lot for all the answers, explanations and your time.

    Yours sincerely...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Returning pointers to locally allocated memory
    in C
    I still don't know why, but this code seems to be working. Not 100% shure, but it gives me the right string every time :)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char * prepareMessage(char str[]) {
    	int i=0, j=0, state=0;
    	char * new;
    	
    	char * pMsgStart = strstr(str, "new ");
    	if(pMsgStart!=NULL) {
    		int i = (strstr(str,
    ...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Returning pointers to locally allocated memory
    in C
    Ok. But it's not much use if nothing is returned... It's something with the fgets i think... If I use a plain array like this:

    Code:
    char inputBuffer[200] = "new \"message\"";
    char * new prepareMessage(inputBuffer);
    it works, but the value comes from fgets() like this:

    Code:
    char inputBuffer[200] = "";
    	char * tmp = malloc(200);
    	
    	do {
    ...
    See more | Go to post

    Leave a comment:


  • krreks
    replied to Returning pointers to locally allocated memory
    in C
    Code:
    char * prepareMessage(char str[]) {
    	int i=0;
    	char * new;
    	
    	char * pMsgStart = strstr(str, "new ");
    	if(pMsgStart!=NULL) {
    		int i = (strstr(str, "new ")-&str[0])+5;
    		new = malloc(strlen(str)-i);
    		strcpy(new, (str+i));
    		if(new[strlen(new)-1] == '"') {
    			new[strlen(new)-1] = '\0';
    			return new;
    		} else {
    			return NULL;
    ...
    See more | Go to post

    Leave a comment:


  • krreks
    started a topic Returning pointers to locally allocated memory
    in C

    Returning pointers to locally allocated memory

    I know it's not "best practice" returning pointers to local variables, but in this case I think i need it.

    I'm writing a method to remove some characters at the beginning and some at the end of a string and return the result as a pointer to memory allocated in the function.

    I developed the function separately, it worked, but when implemented with the rest of the code it doesn't return anything... It seems...
    See more | Go to post
No activity results to display
Show More
Working...