Jump to content


Photo

Standard C problem


  • Please log in to reply
13 replies to this topic

#1 Holy Cow

Holy Cow

    Advanced Member

  • Salty Members
  • 60 posts

Posted 02 May 2012 - 11:01 PM

I have a problem for 2 days now I haven't been able to exactly figure out. I have a txt file that his a list of stuff divided by semicolons (; character) and at the end it has a newline character for the next list, just like so:

...other stuff...;S234;S654;P768;P98
...other stuff...;P4;P654;S123;S432
...other stuff...;P90;S673;S879

As you can see it is random how many items we get in each line and what I want to do is to put each item in a linked list according to its line in an array. It has to look like so:

array[0] -> ...other stuff... S234 -points to-> S654 -points to-> P768 etc.
array[1] -> ...other stuff... P4 -points to-> P654 -points to-> S123 etc.
array[2] -> ......

I've made the fgets loop and all works it reads each line etc, but I'm stuck to the internal loop required in order to read each item separately inside the line given by fgets. Since it is random how many items we have in each line I can't put the strtok a predetermined amount of times inside the fgets loop and I still can't figure out what to do in order to condition the internal loop to understand that it has to exit after it finds the newline character (since this is our queue to go to the next line). Let me remind you that through strtok you can cut the whole string off according to the tokens but that does not mean that the last characters of the string array are the same as what you see after a printf("%s"). There are many junk characters thus I cannot check if the last char is a newline character...

#2 dada

dada

    VILLAIN

  • Administrators
  • 17464 posts
  • Locationsuperhell

Posted 02 May 2012 - 11:02 PM

You should post the code you have so far.

Also, right now the programming forum is kind of quiet, if you can't get an answer you might want to check out our programming irc channel. (#web @ irc.esper.net)


#3 Holy Cow

Holy Cow

    Advanced Member

  • Salty Members
  • 60 posts

Posted 02 May 2012 - 11:22 PM

     FILE *fp;
     char line[200],dump[200];
     int entries,counter=0;
     char *pch;


     fp=fopen("a.txt","r");
     fgets(line,3,fp);
     entries=atoi(line);
     
     CARD table[entries]; //CREATING BASE TABLE FOR CARDS
     
     while (fgets(line,200,fp) != NULL && feof(fp) == 0){
           pch=strtok(line,";");
           strcpy(table[counter].code,pch);
           pch=strtok(NULL,";");
           table[counter].year=atoi(pch);
           pch=strtok(NULL,";");
           table[counter].day=atoi(pch);
           pch=strtok(NULL,";");
           table[counter].cost=atof(pch);
           pch=strtok(NULL,";");
           table[counter].shop_id=atoi(pch);
           pch=strtok(NULL,";");
           
           table[counter].next_item=(PRODUCTP) malloc(sizeof(PRODUCT));
           strcpy(table[counter].next_item->id,pch);
     
     }


After the table[counter].shop_id comes the part i'm talking about. I found out how to remove the '\n' character from the whole line (through using strtok with sole delimiter the '\n' char) but that still does not give me a clue about how I will know how many items to look for and make my loop...

#4 dada

dada

    VILLAIN

  • Administrators
  • 17464 posts
  • Locationsuperhell

Posted 02 May 2012 - 11:23 PM

I wish I could help but I don't program C. :(

Hopefully someone else will come along.


#5 Warped655

Warped655

    Scanner

  • Salty Members
  • 3516 posts
  • LocationSouthern Indiana (unfortunately)

Posted 02 May 2012 - 11:54 PM

More of a C# person myself. I agree with Dada I would even suggest linking this topic on there so maybe you get responses and if someone else comes along they can see how you fixed it. (I always hate looking something up and finding a "fixed" problem in forums but the actual fix isn't explained.)

I'm generally pretty bad with lists/pointers of any sort in C and C++ and kind of rusty in C++ in general so I can't really help much. But I'll see if I can't understand by going over it a bit.

Ed's a bit of a programmer but I don't think he uses C or C++ at all.

sig.gif


#6 Holy Cow

Holy Cow

    Advanced Member

  • Salty Members
  • 60 posts

Posted 03 May 2012 - 12:10 AM

RRRRAAAAHHHH!! What's up with gamingw! It used to be full of so many programmers you could get answers on Cobol questions! Hell this is where I started to learn how to program before we were even taught what a variable is in school. Its been my go to place for anything programming related cause the community rocks but its numbers seem to have narrowed down alot :(

#7 Warped655

Warped655

    Scanner

  • Salty Members
  • 3516 posts
  • LocationSouthern Indiana (unfortunately)

Posted 03 May 2012 - 12:18 AM

Yeah, but the number of people in GW/SW has been ever so slightly increasing in members since its drop.

It could just be the case that a some of the currently posting members are programmers but just don't mention it. I'm not entirely sure of this.

sig.gif


#8 Biggles

Biggles

    Advanced Member

  • Salty Members
  • 984 posts
  • LocationNew Zealand

Posted 03 May 2012 - 12:43 AM

i'm not sure if i'm reading you right, but it looks like you're going line by line. strtok manpage says it will return null after the last token in a string, so you should be able to figure out you're at the last token by that. then you can just strip the \n should it be undesirable since the strings strtok sends back are null-terminated.

i'm not a big C person though so I'm effectively just reading the man page for you, but the clarification would likely make it easier for someone more experienced with the language anyways.

#9 Holy Cow

Holy Cow

    Advanced Member

  • Salty Members
  • 60 posts

Posted 03 May 2012 - 05:57 PM

I'm pretty sure this is the solution that I was looking for... Either my english failed me or I was careless when reading about strtok cause I do not remember anything about it behaving specifically after the last token...

#10 Warped655

Warped655

    Scanner

  • Salty Members
  • 3516 posts
  • LocationSouthern Indiana (unfortunately)

Posted 03 May 2012 - 06:11 PM

Assuming it is the solution you were looking for, this was the first programming issue fixed in the programming section of the forum in a long time. SW/GW HISTORY IS MADE? By Biggles?

sig.gif


#11 Holy Cow

Holy Cow

    Advanced Member

  • Salty Members
  • 60 posts

Posted 03 May 2012 - 10:19 PM

Just to hype it up, the solution actually worked ;)

Giving (pch=strtok(str,";"))!=NULL as a condition in the while string just makes it exit by itself when it encounters the end of the line. Thanks for pointing that out Biggles I totally missed it.

Programming is still not dead it seems :D

#12 Carrion Crow

Carrion Crow

    Advanced Member

  • Salty Members
  • 5038 posts

Posted 06 May 2012 - 07:02 PM

Ed's a bit of a programmer but I don't think he uses C or C++ at all.


I have used them both extensively in the past for my degree. I'm using C to program Arduinos at the moment but I'm not doing any stuff that requires malloc and this pleases me greatly. That module I managed to scrape through with a bare understanding of pointers and memory allocation. More of a C# person myself also.

#13 Biggles

Biggles

    Advanced Member

  • Salty Members
  • 984 posts
  • LocationNew Zealand

Posted 06 May 2012 - 10:56 PM

I seem to remember being warned by profs and others not to use malloc in production code because it's slow or insecure or something. I don't know you get around it aside from c99 variable length arrays though.

p.s. C++ is the devil, STL is turing-complete.

#14 Swordfish

Swordfish

    Advanced Member

  • Salty Members
  • 2902 posts

Posted 22 May 2012 - 06:23 PM

The only thing I know of Arduinos is people using them to make floppy disk music.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users