Thread: Help Reading a file...

  1. #1
    Registered User
    Join Date
    Nov 2010

    Help Reading a file...

    Hello people, Im new to this forum, but just from reading posts I've learned a lot.
    I've got a quick question:

    So I want to create a file that goes like:
    In a file called Users
    <id_user>id number of the user</id_user>
    <name>name of the user</name>
    <pass>password of the user</pass>
    <status>user status(active or non-active)</status>
    //*Uhm I dont know what would be easier, creating a file for each user or just create one big file with all the users..*///
    Id like to know how to read just something specific from that file, for example; if I want to read the name of the user without the <> parameters how could I do that and print it on the screen?
    Please I'd appreciate it very much if you could help out

  2. #2
    Registered User
    Join Date
    Sep 2006
    Welcome to the forum, RoRo!

    What I believe you want to do is:

    1) input each line of the text file, and in that line, remove the <> kind of stuff

    2) you want to use a struct (a record), with members for the various parts dealing with each entry (members of the struct, or fields in the record).

    3) and put your cleaned up data into said struct.

    4) then write out the data as a struct. That will allow it to be efficiently dealt with in the future.

    The alternative would be a simple text file with say, each line of text, having the same format:



    name, handle, url, sex

    This latter has more flexibility, but using struct with members, eases some tasks dealing with the data, greatly - primarily by keeping the data organized better.

    One file is preferable.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    You basically want an XML data file/files. Personally I would recommend one file with several users, especially if you expect there to be more than a few users. There is a library called 'expat' (The Expat XML Parser) that helps you do XML parsing in C, however, it can be a bit tricky, especially if you're not familiar or comfortable with function pointers.

    If you don't really need the structure of XML (I don't think you do), you could try a slightly simpler format, such as CSV: Comma-separated values - Wikipedia, the free encyclopedia. This is actually closer to the format of the password and shadow files that contain similar info on Unix/Linux systems.

    Lastly, depending on the end goal of your program, there are languages that will make processing such files (XML or CSV or whatever) much, much easier. PHP certainly comes to mind, there may be others, but I speak about as many computer languages as I do human, which is to say about 1.5.

    You may also want to consider how often this data is likely to change. If it's going to change often, by adding, deleting and modifying user entries, you may want to consider a database like mysql, postgresql, etc. instead of a flat text file, as these manipulations will get very tedious and disk-intensive otherwise.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Hey thanks for the warm welcome!
    So yeah I was thinking about using structures; but Im not really that familiar with structures(the usage of pointers is still a little blurry :/) It seems structures do waste less memory
    If I'd choose doing it as structure how should it be started out?
    thanks again guys

  5. #5
    Registered User
    Join Date
    Sep 2006
    Above the first function in your program, you prototype the struct, so it's known to every function (it's global in scope):

    Your include files up here, then:
    struct user {
       int userNum;
      char name[40];
      char pword[20];
    int main(void) {
      struct user users[500];  //an array of 500 user structs
      FILE *fp;
      int i;
      return 0;
    Where structs really shine is when you have several fields in the struct (and field == struct member), and you can load a boatload of these structs into an array and do all your searching, sorting, editing, etc., with each record having it's fields right there, easy to work on:

    users[i].name is sooo much nicer than:

    users[i][N] (where N is the column number than the name field begins on - was it 7 or 10, maybe 9?).

    I have a "rule" that after two items that belong (are owned by) something, it gets a struct. Cuts down errors in coding, right to the bone.

    There's a trick to making it easy to use as a flat file - use an index. In this case, an index is an array of it's that you set up just like this:
    for(i=0;i<MAX; i++)
      idx[i] = i;
    Simple no?

    Then, instead of sorting your data, you sort the index, and instead of say showing your sorted data, you show your data THROUGH your sorted index.

    A simple substitution sort, using an index array, (sorting by id number), looks like this:

    for(i=0;i<MAX-1;i++) {
      for(j=0;j<MAX; j++) {
        if(users[index[i]].id > users[index[j]].id) {
          temp = index[i];
          index[j] = temp;
    So the data is never moved, it's never sorted. You just keep adding new records to the end of the file using append file mode. But it looks like it's sorted, and you can print it out like it's sorted, do binary searches (which require sorted data), etc.

    Pretty cool! I don't believe you save memory with structs - you may lose a touch due to padding that the compiler will automatically do (but it's not much). It's a lifesaver for organization - that's it's whole purpose.
    Last edited by Adak; 11-19-2010 at 03:08 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010

    Pointers still not that clear to me..

    Pretty useful, so... For instance:
    I want the structure to be asked in this way:
    Terminal:"Hello new user, won't you please give me your name?"
    User: "Jimi Hendrix"
    Terminal:"Hello Jimi, Wont you give me your status."
    Terminal:"Ok, your account number is 'AccNum', won't you tell me your password?
    User: "123abc"
    Terminal:"Ok, so is this the correct information?
    Name: Jimi Hendrix
    Status: Active
    Account Number: 'AccNum'
    Password: '123abc'
    Terminal: "Thank you Jimi!"
    How should I code it? Im a little slow guys bare with me; Pointers are still not that clear to me;
    Much appreciated!

    POST EDIT///////
    AccNum is a number generated depending on how many users there are.
    Last edited by RoRo; 11-23-2010 at 07:16 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    #define TSIZE 50
    struct Medico
      int Id_Doc[TSIZE];
      char Nombre[TSIZE];
      char Especialidad[TSIZE];
      char Estado[TSIZE];
     struct Medico*next;
    int main(void)
       struct Medico * head = NULL;
       struct Medico * prev, * current;
       char input[TSIZE];
       puts("Que tal nuevo usuario, porfavor dame tu nombre ");
       while (gets(input) != NULL && input[0] != '\0')
           current = (struct Medico *) malloc(sizeof(struct Medico));
           if (head == NULL)
    	 head = current;
    	   prev->next = NULL;
    	   strcpy(current->Nombre, input);
    	   puts("Dame tu Especialidad");
    	   scanf("%d", &current->Especialidad);
    	   while(getchar() != '\n')
    	   puts("Quiere registrar a alguien mas? En ese caso deme su nombre, si no dejar en blanco y presionar enter");
         printf("No data entered. ");
       current = head;
           printf("Doctor: %s Especialidad: %s ", current->Nombre, current->Especialidad);
       while (current !=NULL)
       return 0;
    Thats the code I've got so far just to ask Name and Specialty for a doctor(for example)
    Id_Doc = accnum
    So im just asking for name and specialty, how could I make the terminal ask for the rest of the arguments...?

  8. #8
    Registered User
    Join Date
    Nov 2010


    oh and back to files.
    what would the parameters be if I have a file.txt

    <name>John Fitzpatrick</name>

    How could I make the restriction to read what is just between <name></name> parameters? and how could I create a counter that each time a user registers it goes adds up 1, so next user should be 0002?

    Hahaha^^thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

Tags for this Thread