Advice requested, Code makes sense to me, not compiler

This is a discussion on Advice requested, Code makes sense to me, not compiler within the C Programming forums, part of the General Programming Boards category; so, if im understanding correctly, instead of trying to mess around with file pointers, i should load all of the ...

  1. #31
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    so, if im understanding correctly, instead of trying to mess around with file pointers, i should load all of the files into the program and handle them internally?

  2. #32
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by andrew.bolster View Post
    so, if im understanding correctly, instead of trying to mess around with file pointers, i should load all of the files into the program and handle them internally?
    The point is that you're trying to make a copy of the file handle and expect only one handle to be modified.
    You need to understand how files work, that's all. Consider reading the reply below.


    Yes, foo2 makes a local copy of the data and assigns it to the local copy (sorry, I actually forgot to modify pLocalStruct instead of pStruct; it was a typo - of course both functions should modify pLocalStruct instead of pStruct).
    So, it's before foo1 -> 50, after -> 1, before foo2 -> 50, after foo2 -> 50.

    What you tried to do in your code is the same as foo1, so the both points pointed to the same data which was modified by the file functions.

    Now, I'm not suggesting you should make a copy of the FILE structure. No, the solution to your problem is either opening the file twice (it's valid to have two or more handles to the same file) or simply rewind the position in the file to the beginning again after you read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    take it theres no easy way to rewind other than (for howmanytimestherehasbeenanoperation)pointer--; ? how can the number of pointer operations be counted?

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You rewind a file using the aptly-named function rewind(fileptrgoeshere). It sets the current file read position to the beginning of the file.

  5. #35
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    apt indeed, im asumming thats called after each function call for the pointer?

  6. #36
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    It sets the file position to the beginning of the file. Use it whenever you need to read from the beginning of the file.
    There's also fseek, which is handy for binary files, but can also be used to seek to the beginning or end of a file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by andrew.bolster View Post
    apt indeed, im asumming thats called after each function call for the pointer?
    You use it whenever you want to make sure you're reading from the beginning of the file.

  8. #38
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    ok, new code enclosed, its working. Thankyou for all your help, I'll continue posting code iterations.
    Attached Files Attached Files

  9. #39
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    one quick question, can the rewinds be done within the function calls i.e rewind(ip) ?

  10. #40
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Yes, no problem.
    Main itself is a function, so if it works in main, it works everywhere else. The same goes for any other type of code.
    Last edited by Elysia; 01-05-2008 at 04:51 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    from that response i assumme that file pointers are kinda global?
    unrelated question : fgets takes the entire line including the newline character. Any easy way of dropping the last character? i tried adding a \b after each %s in the final printf but that was gonna be a long shot anyway.

  12. #42
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by andrew.bolster View Post
    from that response i assumme that file pointers are kinda global?
    You take the return value from a function, how can that be global? Unless it's something else you're referring to?

    unrelated question : fgets takes the entire line including the newline character. Any easy way of dropping the last character? i tried adding a \b after each %s in the final printf but that was gonna be a long shot anyway.
    Need help with altering info on a txt file
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    ok, that got rid of the new line but now its overwriting the following characters, eg

    string1="banana\n"
    string2="orange\n"

    after running that if statement on both and printffing them both separated by a space you end up with someting like

    bannge

    :S

  14. #44
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Well, show the code where it's happening.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    i moved u code you suggested from the link into the getword function

    Code:
    const char *getword(FILE* file, int max)
    {
    	FILE *ip;
    	ip=file;
    	int i,j,size;
    	char c;
    	char buffer[20];
    	
    	//printf("random %d\n",rand());
    	//printf("wordno %d\n",(getword_no(ip)));
    	
    	j=(rand()%max);			//this restricts the random number to within the length of the file
    	//printf("Random number=%d",j);
    	for(i=0;i<j;i++)			
    	{
    		fgets(buffer,sizeof(buffer),ip);
    		//printf("getword\n");
    	}
    	fgets(buffer,sizeof(buffer),ip);
    	size=strlen(buffer);
    	printf("Length:%d, String: %s",size, buffer);
    	if (buffer[size - 1] == '\n') buffer[size - 1] = '\0';
    	
    	return buffer;
    }
    the printf statement is

    Code:
    printf("\nA %s %s \n--------------------\n A %s %s %s %s the %s %s \n %s the %s %s 's \n The %s %s %s a %s %s\n"
    		,adjective[0],noun[0]
    		,adjective[0],noun[0],verb[1],preposition[0],adjective[1],noun[1]
    		,adverb[0],noun[0],verb[1]
    		,noun[1],verb[2],preposition[1],adjective[2],noun[2]);

    and example output is
    Code:
       cup
    --------------------
     heatclear
     's celply
     inkunnyt

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  2. my code is messed up says the compiler
    By 0927 in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2002, 12:59 PM
  3. Replies: 4
    Last Post: 01-15-2002, 11:04 PM
  4. Replies: 3
    Last Post: 11-04-2001, 02:53 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

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