file question..

This is a discussion on file question.. within the C Programming forums, part of the General Programming Boards category; i have two files one is a list of guests from the brides side the second is a list of ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    file question..

    i have two files
    one is a list of guests from the brides side
    the second is a list of guests from the grooms side
    each line in the file has
    first name - 15 chars
    last name - 15 chars
    age - 3 chars
    comments - 30 chars
    the files are sorted by last name in alphabetical order and for that last name
    it is sorted by firs name
    notice that the last name comes after the first name
    for example

    dany smith 15 gbgnh
    rebeca smith 30 bgbg
    susana smith 5 gbgf

    i need to create two new files the first one which has the list guests
    which appeared in both given files and the second one has the rest

    my main problem is that i am not allowed to copy the file into a structure
    (no arrays linked list tree other file)

    how the F am i supposed to do it without breaking the file into a structure??
    and compare them??

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    how the F am i supposed to do it without breaking the file into a structure??
    Just use a string and your functions extract the relevant details with fscanf. You could use delimiters other than spaces if that makes it easier (eg. "dany:smith:15:gbgnh").
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant see what you are saying
    if i want to compare names
    i take the first 10 chars from each file
    i need to compare them some how
    fgets requires me to present a place where i put these 10 chars
    ??

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    i cant see what you are saying
    I just noticed "no arrays" -- does that mean no char arrays, or no arrays of strings?

    If you can't use a char array, wow is that tedious. You will have to use fgetc() or fread() byte by byte and have a function that for every line in one file (use the \n) goes through all of the other file (byte by byte) looking for a match. Very silly stuff.

    I would verify that you can't use a string for storage first, your prof has really outdone himself, ridiculousness wise, if that is the case.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i get a solution to a similar proglem
    and they use this weird lines all the way
    what they mean??

    Code:
    fscanf(fold,"%29[^$]%10s%38[^$]%*c",details1old,teold,details2old);
    Last edited by transgalactic2; 03-25-2009 at 12:47 PM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Sure, but this is still using strings. If you cannot use a char array, there is no point in using a scanf function. You will have to use fgetc and work with the int value of each character.

    If you can use a char array, then do use fgets and fscanf.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i know that
    but thats what is written
    so its allowed
    but i cant figure our this fscanf line
    Code:
    fscanf(fold,"%29[^$]%10s%38[^$]%*c",details1old,teold,details2old);

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by transgalactic2 View Post
    but i cant figure our this fscanf line
    how many links from 172000 brought by google you have already studied?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by transgalactic2 View Post
    Code:
    fscanf(fold,"%29[^$]%10s%38[^$]%*c",details1old,teold,details2old);
    Read up to 29 chars into variable details1old unless a '$' is found earlier, then read up to 10 chars into teold unless a whitespace is found earlier, then read up to 38 chars into details2old unless '$' is found earlier, and skip the rest
    I think you should google scanf format specifiers.
    Kurt
    Last edited by ZuK; 03-25-2009 at 01:14 PM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Wow, nice job by Zuk. But transgalactic2, don't get distracted. Based on your OP, if you can use strings, you don't need anything more complicated that this:

    Code:
    FILE *in;
    char buffer[64];
    while ((fgets(buffer,63,in)) {
           fscanf(buffer, "%s %s %d %s", fname, lname, age, comment);
    }
    And in fact 15+15+30+3=63, so your prof must have been thinking of this nice even block size.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why do you pass 63 to fgets?

    should be sizeof buffer if it is so hard to calculate buffer length

    and second call should be sscanf of course

    also age probably misses &
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by MK27 View Post
    Wow, nice job by Zuk. But transgalactic2, don't get distracted. Based on your OP, if you can use strings, you don't need anything more complicated that this:

    Code:
    FILE *in;
    char buffer[64];
    while ((fgets(buffer,63,in)) {
           fscanf(buffer, "%s %s %d %s", fname, lname, age, comment);
    }
    And in fact 15+15+30+3=63, so your prof must have been thinking of this nice even block size.
    what the while condition means??

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by transgalactic2 View Post
    what the while condition means??
    what fgets returns?

    when you start using google?

    do you want to learn something?

    do you love to irritate people?

    why are you trolling so much?

    why don't you choose another forum for meaningless postings?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You're a character, vart. Nuff said.

    fgets will return a null pointer at EOF. That's what I'm testing for. Maybe you noticed this is the "C programming forum"?

    [edit] Oh no! I just noticed this was in response to trans2's post to my post. I am very sorry vart, hope there's no hard feelings...I guess this makes me all three of impatient, ignorant, and PARANOID.

    Sorry sorry sorry.

    @trans2 -- read the documentation, fer chrissake
    [edit] yes, it should be sscanf, whoops. I used 63 because fgets adds a null terminator. and &with the %d. sorry sorry sorry
    Last edited by MK27; 03-25-2009 at 01:47 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21