Problem

This is a discussion on Problem within the C Programming forums, part of the General Programming Boards category; So I have been attempting to make a function which will take a file in the format... X 1111111100000000000000000000111111111111... 1111111111111111111111111111111111000000... ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    18

    Problem

    So I have been attempting to make a function which will take a file in the format...

    X
    1111111100000000000000000000111111111111...
    1111111111111111111111111111111111000000...
    1111111111111111111111111111111111111111...
    1100000000000000000000000000000000000000...
    ...

    1111111111111100000000000000000000000000...
    ....

    Where X is the number of blocks of numbers, each of the blocks are 32 lines, and each of the lines in the blocks are 310 characters long. I want to have this function read each of these blocks, determine if there are any instances within each block where the sequence 01 occurs, and then if that is the case place an "R" in the empty line after the block.

    Currently I have...

    Code:
    void prune(void) /*Prune results*/
    {
        int i, bi, ini, reps; /*Counter, branch counter, inner counter, # of sims*/
        char str[311], rev; /*Line by line string, reversal detector*/
        FILE *fs;
        if((fs = fopen("compreadasym32.txt", "a+")) == NULL) { /*Complex computer readable summary file*/
            printf("Error opening file.\n");
            exit(1);
        }
        fscanf(fs, "%d", &reps);
        for(i=0; i<reps; i++) {
            for(bi=0; bi<32; bi++) {
                fgets(str, 311, fs);
                for(ini=1; ini<310; ini++) if((str[ini]-str[ini-1])==1) rev++;
            }
            if(!rev) fprintf(fs, "R");
            rev=0;
        }
        fclose(fs);
    }
    This doesn't alter the file in any way when run.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    I would declare one array of 32*310+1 bytes to hold the whole block (+1 for the null), and a second of 312 (310 digits plus new line plus null). Then I would call use fgets 32 times to read in each line into the small buffer and concatenate that line onto the end of the big one. Just remember to trim the new line from fgets before concatenating it onto the end of the string. Then you can use strstr to find your "01".

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Well in-place edits of a text file are not supported.

    So you need to read from one file, and write to another, making a copy of everthing you read, and occasionally outputting an R as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Note that the fprintf output is appended to the end of the file. It is not inserted in the place you are reading from.
    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
    Nov 2011
    Posts
    18
    Okay so I tried to put the results from the test into a new file and so I changed the code to this.

    Code:
    void prune(void) /*Prune results*/{
    	int i, bi, ini, reps; /*Counter, branch counter, inner counter, # of sims*/
    	char str[311], rev; /*Line by line string, reversal detector*/
    	FILE *fs;
    	if((fs = fopen("compreadasym32.txt", "a+")) == NULL) { /*Complex computer readable summary file*/
    		printf("Error opening file.\n");
    		exit(1);
    	}
    	FILE *ft;
    	if((ft = fopen("readassym32.txt", "a+")) == NULL) {
    		printf("Error opening file.\n");
    		exit(1);
    	}
    	fscanf(fs, "%d", &reps);
    	for(i=0; i<reps; i++) {
    		for(bi=0; bi<32; bi++) {
    			fgets(str, 311, fs);
    			for(ini=1; ini<310; ini++) if((str[ini]-str[ini-1])==1) rev++;
    		}
    		if(!rev) fprintf(ft, "R%d\n", i);
    		rev=0;
    	}
    	fclose(fs);
    }
    It does make the other file and reports a column of R_ for some but not all of the block numbers. The only problem is it it wrong about which ones have "01".

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How can you tell? You are not writing out the contents of the original file.
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    This is only one function of the program. I checked through the file being read myself and the results are wrong.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Keep it simple!

    "r" for reading, "w" for writing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 04:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM

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