Reading file.txt - needs help

This is a discussion on Reading file.txt - needs help within the C Programming forums, part of the General Programming Boards category; hello there.. i am having a small problem.. i have two input files.. groups.txt and command.txt,.. groups.txt has 4 lines ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    Reading file.txt - needs help

    hello there..
    i am having a small problem..

    i have two input files.. groups.txt and command.txt,..
    groups.txt has 4 lines in it..

    for example:


    u 7 1 2 3 5 8 9 10
    a 5 3 5 8 9 10
    b 3 1 8 9
    c 2 3 5

    blue = name
    red = size
    the rest are the numbers seperated with space.

    i want to do a command that

    do what in command.txt

    which is

    aU~b*c

    1. ~b = 2 3 5 8 10

    2. aU~b = 1 3 5 8 9 10

    3. aU~b * c = 1 8 9 10 ====> to write in output.txt

    , so far i dont know what to do..

    i need to know how to check each line in the groups.txt ,.. for each command from 1-3.

    how to do that?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Right now I want a polymorphable file browser done completely in openGL -- but let's slow down a minute.

    It sounds like you can already read both files and are wondering how to use the input for some purpose, right? Maybe...your post is a little unclear.

    Can you read in the file and print each line to the screen?
    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
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    let me explain it again..

    i need to build a function that READS 2 FILES

    1. groups.txt
    2. expressions.txt

    and the output in output.txt

    1. groups.txt
    contains 4 lines of 4 groups, Universal group, and a b and c.
    each line in this files made of "the name of the group" "the size of the group" "then the numbers in the group"

    Code:
    u 7 1 2 3 5 8 9 10
    a 5 3 5 8 9 10
    b 3 1 8 9
    c 2 3 5
    2. expressions.txt
    contains a command that i have to do with the above groups.

    Code:
    aU~b*c
    * = symmetric difference
    U = unite

    for example to do the command from left to right:

    first to do aU~b
    then aU~b*c

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, it looks like you have prepared a set of specifications, which is what you would give someone else if you wanted them to write a program for you.

    Since that is not the case, you have made your own task somewhat easier anyway. Now you just have to do it! But slow down again...

    Can you open one file and read it in C? If you cannot do any or some of the things asked for in your specification, start with what you believe is the most appropriate mystery and decide how to solve it.

    That might include asking some questions on the forum, perhaps, but start with just one. Your program is obviously a serious of relatively simple steps.
    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
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    i know how to start my friend..
    if you can solve it be my guest,..

    i just got stuck,.. i used to read lines from files.. when there's only 1 line and put them into array..

    but with 4 lines.. i don't know how

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sanfor View Post
    i know how to start my friend..
    if you can solve it be my guest,..

    i just got stuck,.. i used to read lines from files.. when there's only 1 line and put them into array..

    but with 4 lines.. i don't know how
    Why do you believe it's different? (Hint: it isn't.) (Double hint: especially in this case, where line breaks are important, which means you're reading one line at a time anyway.)

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sanfor View Post
    i know how to start my friend..
    if you can solve it be my guest,..

    i just got stuck,.. i used to read lines from files.. when there's only 1 line and put them into array..

    but with 4 lines.. i don't know how
    Well, since each line in one of the files is a command, you probably want to read a single line at a time from that file (leave the file open, and you pick up where you left off automatically) and then work with that line.
    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
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    i didn't get you..

    Code:
    void blah (char *groups,char *expressions,char *ouput)
    {
    FILE *IN1;
    FILE *IN2;
    FILE *OUT;
    
    int size1,size2,size3,size4,i,j;
    int *u,*a,*b,*c;
    
    IN1=fopen(groups,"r");
    IN2=fopen(expressions,"r");
    OUT=fopen(output,"w");
    
    fscanf(IN1,"%d",&size1); ///////////// i dont know here,.. i want to limit a size for each array which will contain 4 groups array u = universal, array a = a group , array b = group b,.. and c for array c.. (is it neccessary?)
    u=(int*)malloc(sizeof(int)*size1); //
    
    while (!feof(IN1))
    {
    fscanf(IN1,"%d",u[i]);
    i++;
    }
    
    
    }
    this is what i got so far,.. i dont know how todeal with 4 lined txt file..

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What you've got leans toward unorganized mess. Indent your code!!!!

    Anyway, here's an idea for you. Use a seperate function to read a single line from an open file. The advantages:

    1) you don't have to repeat too much code.
    2) you can use it with any open file.
    3) it's probably the most "optimized" choice, in terms of system resources.

    Here's an example that opens two files and alternates printing one line from each of them to show you what I mean:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int get_a_line (FILE *strIN, char *line) {
    	int i=0;
    	int ch;
    	if (strIN==NULL) return -1;
    	while ((ch=fgetc(strIN)) > 0) {
    		if (ch=='\n') {   /* the end of the line */
    			line[i]='\0';  /* important to add this */
    			return i; }
    		else line[i]=ch;
    		i++;
    	}
    	line[i]='\0'; /* because the last line probably won't have a newline */
    	return i;	/* "i" will still be zero if file is finished */
    }
    
    
    int main (int argc, char *argv[]) {
    	int i;
    	char string[256]; /* this needs to be an appropriate length */
    	FILE *IN1=fopen(argv[1],"r"); 
    	FILE *IN2=fopen(argv[2],"r"); /* if you don't provide two valid filenames there will be a problem */
    
    	for (i=0;i<50;i++) {
    		if (get_a_line(IN1,string)>0) printf("from %s: %s\n",argv[1],string);
    		if (get_a_line(IN2,string)>0) printf("from %s: %s\n",argv[2],string);
    	}
    	return 0; /* C closes the files */
    }
    You use it like this, if the executable is called "example"
    example firstnames.txt dictionary.txt
    and it produces output like this:
    Code:
    from firstnames.txt: Susan
    from dictionary.txt: abaser
    from firstnames.txt: Clem
    from dictionary.txt: abasers
    from firstnames.txt: Atsumi
    from dictionary.txt: abases
    from firstnames.txt: April
    from dictionary.txt: abash
    from firstnames.txt: William
    from dictionary.txt: abashed
    from firstnames.txt: Moses
    from dictionary.txt: abashes
    from firstnames.txt: Arthur
    from dictionary.txt: abashing
    from firstnames.txt: Peter
    from dictionary.txt: abasia
    In your situation, you can then apply sscanf (not fscanf) to the string in order to extract your information.

    Do you see how much simpler this will make everything? And without sacrificing any efficiency in the actual program.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 11:36 PM
  5. Reading a file.txt
    By neo6132 in forum C Programming
    Replies: 4
    Last Post: 03-11-2003, 08:06 AM

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