Page 1 of 1

Help with C

Posted: 30 Jan 2013, 09:19
by minnerthecat
Hello all, minnerthecat here with a question for anyone familiar with the C programming language.
I'm trying to write a program that reads data from a text document. I'm trying to tokenize the elements and this is where I'm running in to an issue:
- Each line in the text document contains exactly nine "elements" separated by "|" characters that I want to store as tokens in an array.
- I've tried using the strtok() function and specifying "|" as the delimiter, but strtok() also recognizes whitespace as a delimiter whether it's specified or not.
- When strtok() encounters a space, it inserts a NULL value in that element in the array, then ignores the rest of the elements in the line and carries on to the next line.

Does anyone know how to make strtok() ignore whitespace as a delimiter, or know of another function that will only tokenize based on specified delimiters?

Re: Help with C

Posted: 30 Jan 2013, 09:28
by captainnemo3
My experience with programming is limited, and been a while since I actually used it, but you're just grabbing one character at a time anyway, you tried either if else or case exception? if char = "|" go to next character, else if store it. (Perhaps I'm way oversimplifying what you're trying to do, in which case ignore me.)

Re: Help with C

Posted: 30 Jan 2013, 10:00
by J4Numbers
Have you tried specifying it as a non-acceptable character instead?
I haven't done C myself, but I think that you should be able to use (!&nbsp) or something similar.
Just an idea.

Re: Help with C

Posted: 30 Jan 2013, 14:02
by minnerthecat
Thanks for the suggestions guys! I did end up finding out what the problem was and it was something completely different than I thought; it actually had nothing to do with strtok(). I'll post it here for future reference:
- I was reading in the data line by line using fscanf() set up like so:

fscanf(data, "%s", string)

(where data was the name of my FILE pointer, and string was an array of chars.)

- What I didn't realize was that the "%s" will only copy characters until it encounters whitespace, therefore, the program wouldn't read any of the entries in the first line after the first space had occured. (instead, it treated them as the next line of data).

- The problem was remedied by using fgets() instead. I set it up like so:

fgets(string, WORDMAX, data)

- This allowed the program to read an entire line of the data file properly, spaces and all!

Re: Help with C

Posted: 30 Jan 2013, 16:58
by aeroeng15
I have very limited experience, but I do believe, from my experience with MATLAB, that setting up the fscanf with something like "%s %s %s....." will read in all columns if it is of the given type.

Re: Help with C

Posted: 05 Apr 2013, 09:00
by minnerthecat
Hello all, I'm back with another problem!

I have to design a memory manager in C (similar to Java's automatic memory management), using a Linked List and a buffer, but I'm running into issues accessing parts of the buffer. Here's how the program works:

- Each node in the Linked List represents a block of memory in the buffer. The "offset" variable in the node is the address on the buffer that the block starts at.
- The buffer is just an integer pointer that has a defined amount of memory allocated to it.
- The problem arises in my "retrieveObject" function, which traverses the linked list to find the node that matches the given reference number (an unsigned long). When it finds this node, it uses the "offset" variable of the node to try an access a certain part of the allocated memory in the buffer and return that address as a pointer back to the main function. However, my "retrieveObject" function only seems to be returning a NULL value, as my program ends with a segmentation fault when it reaches the end of "retrieveObject".

Does anyone have any ideas why this is happening? I'll provide the source code for the function in question:
Spoiler! :
Here is the code for retrieveObject():

Code: Select all

void *retrieveObject(Ref ref){
     ptr = head;
     
     int **memPtr = NULL;
     Ref offsetCount = 0;
     
     while((NULL != ptr) && (ptr->refID != ref)){
        ptr = ptr->next;
     }
     offsetCount = ptr->offset;
     if(offsetCount == 0){
        memPtr = &(buffer1);
     }else{
        *memPtr = (buffer1+offsetCount);
     }
     fprintf(stderr,"%p, %p, %lu\n", &buffer1, (buffer1+1), offsetCount);
     return *memPtr; 
  
}
Thanks for your time everyone!
EDIT: ignore the fprintf statement; that was just something I stuck in to help with debugging.