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; I'm trying to hone my C skills and, well, its not going very well. I wanted to write a C ...

  1. #1
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45

    Question Advice requested, Code makes sense to me, not compiler

    I'm trying to hone my C skills and, well, its not going very well.
    I wanted to write a C program that prints out a poem of a fairly basic structure, to practice file operations and pointers.

    The dictionary files referenced are "fake" csv files, in essence they arnt comma separated, they are # separated between words and & separated to denote end of file (not tidy atall but its just an experiment)

    From the output it looks like my pointer operations are going astray somewhere and I've brought this code to a professor at my local uni and he was stumped too.
    Code:
    // PoetryInCode.c : Defines the entry point for the console application.
    //	Output  Mapping:			3*adj=adjective
    //						3*nou=noun
    //						3*ver=verb
    //						1*adv=adverb
    //						2*pre=preposition
    //
    //
    //					"A (adj1) (nou1)"
    //			-----------------------------------
    //			A[n] (adj1) (nou1) (ver1) (pre1) the (adj2) (nou2).
    //			(adv1) the (nou1) (ver2)
    //			The (nou2)(ver3) (pre2) a[n] (adj3) (nou3)
    //
    //
    //functions:
    //		char* openfile()		Opens noun.csv, adjective.csv, verb.csv, adverb.csv or preposition.csv for read
    //								returning a pointer to a pointer array such that each pointer in the array 
    //								points to the file. {*nou,*adj,*ver,*adv,*pre}.
    //
    //		char* getword(char*)	Takes {*nou/*adj/*ver/*adv/*pre} and returns a pointer to the first letter of a string
    //								from a random line of the file specified in openfile().
    //
    //		int makeAn(char*)		Takes a pointer to a string and returns 0 if A is the proper indefinite article and 1 
    //								if it should be An.
    //
    //		int getword_no(FILE*)	returns the number of words in the dictionary file
    //
    //		int getword_max(FILE*)	returns the maximum word length of words in the dictionary file
    //
    //caveats:
    //		to ensure the wordlist can be added to and modified, the wordlists are read in several times during the program
    //		to read the number of words, and the maximum word length, governing the modulus of the random function and the
    //		maximum char[] length respectivly.
    //
    //		when the word lengths are counted, the delimiting character is counted too, so the counter would read above as 
    //		having 6 characters, allowing for the null end of string character in the arrays
    
    
    //#include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int getword_no(FILE *filename)
    {	FILE *ip;
    	ip=filename;
    	int word_number=0;
    	char c='*';
    	int i;
    	//while(c!='&')
    	for(i=0;i<10;i++)				//used as test loop
    	{	printf("Letter %c ",c);
    		printf("No. %d\n ",word_number);
    		c=getc(ip);
    		printf("getc(ip) %c\n",c);
    		if(c=='#')word_number++;
    	}
    	printf("Number of words in dictionary file is %d",word_number);
    	return(word_number);
    }
    int getword_max(FILE *filename)
    {	FILE *ip;
    	ip=filename;
    	int word_length,max_length;
    	char c=' ';
    	word_length=max_length=0;
    	while(c!='&')
    	{
    		c=getc(ip);
    		if(c=='#')
    		{
    			if(word_length>max_length)max_length=word_length;			
    			word_length=0;
    			//printf("getword_max\n");
    		}
    		else{word_length++;}
    	}
    	return(max_length);
    }
    FILE* getword(FILE* file)
    {
    	FILE *ip;
    	ip=file;
    	int i,j;
    	char c;
    	//printf("random %d\n",rand());
    	printf("wordno %d\n",(getword_no(ip)));
    	
    	j=(rand())%(getword_no(ip));			//this restricts the random number to within the length of the file
    	printf("Random number=%d",j);
    	for(i=0;i!=j;)			
    	{
    		if(c=='#')i++;		//runs the pointer foward to the random word
    		c=getc(ip);
    		printf("getword\n");
    	}
    	return(ip);
    }
    int main()
    {
    	//declare file pointers
    	FILE *adj,*nou,*ver,*adv,*pre,*current; 
    	
    	//seed random number generator
    	srand((unsigned)time(NULL));
    	//open files as readable while checking if it exists, if not, quit
    	if((nou=fopen("noun.csv", "r"))==0)
    	{
    		printf("No Nouns\n");
    		exit(EXIT_FAILURE);
    	}
    	if((adj=fopen("adjective.csv", "r"))==0)
    	{
    		printf("No Adjectives\n");
    		exit(EXIT_FAILURE);
    	}
    	if((ver=fopen("verb.csv", "r"))==0)
    	{
    		printf("No Verbs\n");
    		exit(EXIT_FAILURE);
    	}
    	if((adv=fopen("adverb.csv", "r"))==0)
    	{
    		printf("No Adverbs\n");
    		exit(EXIT_FAILURE);
    	}
    	if((pre=fopen("preposition.csv", "r"))==0)
    	{
    		printf("No Prepositions\n");
    		exit(EXIT_FAILURE);
    	}
    /**********************************************************************************************/
    	//get maximum word lengths
    	
    	int adj_max,nou_max,ver_max,adv_max,pre_max,max_max;
    	max_max=0;
    	current = adj;
    	adj_max=getword_max(current);
    		if(adj_max>max_max)max_max=adj_max;
    	current = nou;
    	nou_max=getword_max(current);
    		if(nou_max>max_max)max_max=nou_max;
    	current = ver;
    	ver_max=getword_max(current);
    		if(ver_max>max_max)max_max=ver_max;
    	current = adv;
    	adv_max=getword_max(current);
    		if(adv_max>max_max)max_max=adv_max;
    	current = pre;
    	pre_max=getword_max(current);
    		if(pre_max>max_max)max_max=pre_max;
    	
    	printf("Longest Words:\nAdj=%d\nNou=%d\nVer=%d\nAdv=%d\nPre=%d\nMax=%d\n",adj_max, nou_max, ver_max, adv_max, pre_max,max_max);
    	
    	//declare string arrays[lnumber][length]
    	char adjective[3][adj_max];
    	char noun[3][nou_max];
    	char verb[3][ver_max];
    	char adverb[1][adv_max];
    	char preposition[2][pre_max];
    	char ans[3][2];
    	printf("Random Word %s",getword(adj));
    	
    	//get adjectives
    //	current=adj;
    //	for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
    	
    	//get nouns
    //	current=nou;
    //	for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
    	
    	//get verbs
    //	current = ver;
    //	for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
    	
    	//get adverbs
    //	current = adv;
    //	strcpy(adjective[0],getword[current]);
    	
    	//get prepositions
    //	current = pre;
    //	for(i=0;i<2;i++)strcpy(adjective[i],getword[current]);
    	
    	//form a(n)'s
    //	if (adjective[0][0] == 'a'||adjective[0][0] == 'e'||adjective[0][0] == 'i'||adjective[0][0] == 'o'||adjective[0][0] == 'u')
    //		ans[0]=strcat(ans[0],'n');
    	//print poem
    	
    //					"A (adj1) (nou1)"
    //			-----------------------------------
    //			A[n] (adj1) (nou1) (ver1) (pre1) the (adj2) (nou2).
    //			(adv1) the (nou1) (ver2)
    //			The (nou2)(ver3) (pre2) a[n] (adj3) (nou3)
    	
    	return 0;
    }
    NB i promise that when i get it working I'll tidy up the comments and close the opened files

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    In C89, all variables definitions must come before everything else in the block, so move them up top and don't perform any assignment or do anything else before you define your variables.
    There are also implicit conversions between types, which is usually illegal. Do explicit casts to get rid of warnings, or just change to an appropriate type.
    There are also undeclared or undefined variables that are used, so define them.
    Last edited by Elysia; 01-05-2008 at 02:47 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.

  3. #3
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    I apologise, i should have pointed out i was using gcc on a linux system, the program compiles, it just throws crazy outputs
    Code:
    Longest Words:
    Adj=17
    Nou=14
    Ver=13
    Adv=12
    Pre=12
    Max=17
    Letter * No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Number of words in dictionary file is 0wordno 0
    Letter * No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Letter &#255; No. 0
     getc(ip) &#255;
    Floating point exception (core dumped)
    Last edited by andrew.bolster; 01-05-2008 at 08:34 AM. Reason: old output

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > getc(ip) &#255;
    I think you'll find that &#255; is char 255 in your character map, which is the equivalent of (char)EOF
    My guess is you need to look at how you read the file.

    IMO, you should be using fgets() to read a line, then use another function to tokenise that into comma separated fields.
    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.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Code:
    c=getc(ip);
    getc returns an int, not char. Redeclare c as int in those places.
    Last edited by Elysia; 01-05-2008 at 02:46 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.

  6. #6
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45

    modifications

    redid everything as fgets, it actually makes things alot easier, but the program is still ........ting itself at the getword_no function

    Code:
    // PoetryInCode.c : Defines the entry point for the console application.
    //	Output  Mapping:			3*adj=adjective
    //						3*nou=noun
    //						3*ver=verb
    //						1*adv=adverb
    //						2*pre=preposition
    //
    //
    //					"A (adj1) (nou1)"
    //			-----------------------------------
    //			A[n] (adj1) (nou1) (ver1) (pre1) the (adj2) (nou2).
    //			(adv1) the (nou1) (ver2)
    //			The (nou2)(ver3) (pre2) a[n] (adj3) (nou3)
    //
    //
    //functions:
    //		char* openfile()		Opens noun.csv, adjective.csv, verb.csv, adverb.csv or preposition.csv for read
    //								returning a pointer to a pointer array such that each pointer in the array 
    //								points to the file. {*nou,*adj,*ver,*adv,*pre}.
    //
    //		char* getword(char*)	Takes {*nou/*adj/*ver/*adv/*pre} and returns a pointer to the first letter of a string
    //								from a random line of the file specified in openfile().
    //
    //		int makeAn(char*)		Takes a pointer to a string and returns 0 if A is the proper indefinite article and 1 
    //								if it should be An.
    //
    //		int getword_no(FILE*)	returns the number of words in the dictionary file
    //
    //		int getword_max(FILE*)	returns the maximum word length of words in the dictionary file
    //
    //caveats:
    //		to ensure the wordlist can be added to and modified, the wordlists are read in several times during the program
    //		to read the number of words, and the maximum word length, governing the modulus of the random function and the
    //		maximum char[] length respectivly.
    //
    //		when the word lengths are counted, the delimiting character is counted too, so the counter would read above as 
    //		having 6 characters, allowing for the null end of string character in the arrays
    
    
    //#include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    int getword_no(FILE *filename)
    {	FILE *ip;
    	ip=filename;
    	int word_number=0;
    	char string[20];
    	int i;
    	//for(i=0;i<10;i++)				//used as test loop
    	while(!(feof(ip)))
    	{	
    		//printf("No. %d\n ",word_number);
    		fgets(string,20,ip);
    		printf("%s\n",string);
    		word_number++;
    	}
    	printf("Number of words in dictionary file is %d",word_number);
    	return(word_number);
    }
    int getword_max(FILE *filename)
    {	FILE *ip;
    	ip=filename;
    	int word_length,max_length;
    	char c=' ';
    	char string[20];
    	word_length=max_length=0;
    	while(!(feof(ip)))
    	{
    		fgets(string,20,ip);
    			if(strlen(string)>max_length)max_length=strlen(string);			
    	}
    	return(max_length);
    }
    void getword(FILE* file, char* string)
    {
    	FILE *ip;
    	ip=file;
    	int i,j;
    	char c;
    	//printf("random %d\n",rand());
    	printf("wordno %d\n",(getword_no(ip)));
    	
    	j=(rand())%(getword_no(ip));			//this restricts the random number to within the length of the file
    	printf("Random number=%d",j);
    	for(i=0;i!=j;)			
    	{
    		fgets(NULL,20,ip);
    		//printf("getword\n");
    	}
    	
    }
    int main()
    {
    	//declare file pointers
    	FILE *adj,*nou,*ver,*adv,*pre,*current; 
    	
    	//seed random number generator
    	srand((unsigned)time(NULL));
    	//open files as readable while checking if it exists, if not, quit
    	if((nou=fopen("noun.csv", "r"))==0)
    	{
    		printf("No Nouns\n");
    		exit(EXIT_FAILURE);
    	}
    	if((adj=fopen("adjective.csv", "r"))==0)
    	{
    		printf("No Adjectives\n");
    		exit(EXIT_FAILURE);
    	}
    	if((ver=fopen("verb.csv", "r"))==0)
    	{
    		printf("No Verbs\n");
    		exit(EXIT_FAILURE);
    	}
    	if((adv=fopen("adverb.csv", "r"))==0)
    	{
    		printf("No Adverbs\n");
    		exit(EXIT_FAILURE);
    	}
    	if((pre=fopen("preposition.csv", "r"))==0)
    	{
    		printf("No Prepositions\n");
    		exit(EXIT_FAILURE);
    	}
    /**********************************************************************************************/
    	//get maximum word lengths
    	
    	int adj_max,nou_max,ver_max,adv_max,pre_max,max_max;
    	max_max=0;
    	current = adj;
    	adj_max=getword_max(current);
    		if(adj_max>max_max)max_max=adj_max;
    	current = nou;
    	nou_max=getword_max(current);
    		if(nou_max>max_max)max_max=nou_max;
    	current = ver;
    	ver_max=getword_max(current);
    		if(ver_max>max_max)max_max=ver_max;
    	current = adv;
    	adv_max=getword_max(current);
    		if(adv_max>max_max)max_max=adv_max;
    	current = pre;
    	pre_max=getword_max(current);
    		if(pre_max>max_max)max_max=pre_max;
    	
    	//get maximum word lengths
    	
    	int adj_x,nou_x,ver_x,adv_x,pre_x,max_x;
    	max_x=0;
    	current = adj;
    	adj_x=getword_no(current);
    		if(adj_x>max_x)max_x=adj_x;
    	current = nou;
    	nou_x=getword_no(current);
    		if(nou_x>max_x)max_x=nou_x;
    	current = ver;
    	ver_x=getword_no(current);
    		if(ver_x>max_x)max_x=ver_x;
    	current = adv;
    	adv_x=getword_no(current);
    		if(adv_x>max_x)max_x=adv_x;
    	current = pre;
    	pre_x=getword_no(current);
    		if(pre_x>max_x)max_x=pre_x;
    	
    	printf("Longest Words:\nAdj=%d\nNou=%d\nVer=%d\nAdv=%d\nPre=%d\nMax=%d\n",adj_max, nou_max, ver_max, adv_max, pre_max,max_max);
    	printf("Longest Dictionary:\nAdj=%d\nNou=%d\nVer=%d\nAdv=%d\nPre=%d\nMax=%d\n",adj_x, nou_x, ver_x, adv_x, pre_x,max_x);
    	
    	//declare string arrays[lnumber][length]
    	char adjective[3][adj_max];
    	char noun[3][nou_max];
    	char verb[3][ver_max];
    	char adverb[1][adv_max];
    	char preposition[2][pre_max];
    	char ans[3][2];
    	getword(adj,adjective[0]);
    	
    	//get adjectives
    //	current=adj;
    //	for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
    	
    	//get nouns
    //	current=nou;
    //	for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
    	
    	//get verbs
    //	current = ver;
    //	for(i=0;i<3;i++)strcpy(adjective[i],getword[current]);
    	
    	//get adverbs
    //	current = adv;
    //	strcpy(adjective[0],getword[current]);
    	
    	//get prepositions
    //	current = pre;
    //	for(i=0;i<2;i++)strcpy(adjective[i],getword[current]);
    	
    	//form a(n)'s
    //	if (adjective[0][0] == 'a'||adjective[0][0] == 'e'||adjective[0][0] == 'i'||adjective[0][0] == 'o'||adjective[0][0] == 'u')
    //		ans[0]=strcat(ans[0],'n');
    	//print poem
    	
    //					"A (adj1) (nou1)"
    //			-----------------------------------
    //			A[n] (adj1) (nou1) (ver1) (pre1) the (adj2) (nou2).
    //			(adv1) the (nou1) (ver2)
    //			The (nou2)(ver3) (pre2) a[n] (adj3) (nou3)
    	
    	return 0;
    }
    returning an output of

    Code:
    Number of words in dictionary file is 0Number of words in dictionary file is 0Number of words in dictionary file is 0Number of words in dictionary file is 0Number of words in dictionary file is 0Longest Words:
    Adj=15
    Nou=16
    Ver=15
    Adv=14
    Pre=14
    Max=16
    Longest Dictionary:
    Adj=0
    Nou=0
    Ver=0
    Adv=0
    Pre=0
    Max=0
    Number of words in dictionary file is 0wordno 0
    Floating point exception (core dumped)

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Oops, it seems the code is C99.
    Last edited by Elysia; 01-05-2008 at 02:46 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.

  8. #8
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    sorry, but its compiling on my system, gcc 4.1.2

    What im looking for are logical errors in the getword_no function. as far as i can see, I set up the file pointers correctly, and the word length function works fine, the original file pointers are untouched because they are always copied to a local pointer variable ip before being messed with. So every time they are used, the original file pointers should be the same.

    The same end of file test is used in both the word length and word number functions, but its not even running once.

    Thankyou

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Oops, it seems the code is C99.
    Last edited by Elysia; 01-05-2008 at 02:47 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.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > for(i=0;i!=j;)
    This will either loop forever, or not at all.

    > fgets(NULL,20,ip);
    This will simply crash with a segfault.

    > while(!(feof(ip)))
    Read the FAQ as to why using feof() to control a loop is bad.
    Instead, you should write
    while ( fgets(string,20,ip) != NULL )

    Or better
    while ( fgets(string,sizeof(string),ip) != NULL )
    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.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Even though you're using different names (current, ip, whatever) the pointers are all pointing at the same file structure; so if you want to re-read the file from the beginning you'll have to rewind the file pointer.

    (And if it makes you feel better, it compiles on my system, but with some unused variable warnings and a comparison between signed and unsigned in getword_max.)

  12. #12
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    Enclosed is source file.
    Clean compile, runtime errors as previously mentioned
    Attached Files Attached Files

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Right, it's because you don't follow C89 rules that I'm getting undeclared identifiers. Makes sense now.
    I see C99 in here, actually... makes it somewhat difficult to test, but there are a few things...

    Code:
    int i;
    in
    Code:
    int getword_no(FILE *filename)
    Unreferenced.

    Code:
    if(strlen(string)>max_length)max_length=strlen(string);
    in
    Code:
    int getword_max(FILE *filename)
    Signed and unsigned mismatch. Strlen returns size_t, which is unsigned, but max_length is signed, so make it unsigned!

    Code:
    char c=' ';
    in
    Code:
    int getword_max(FILE *filename)
    Is never used.

    Code:
    void getword(FILE* file, char* string)
    Argument string is never used.

    Code:
    char c;
    in
    Code:
    void getword(FILE* file, char* string)
    Is never used.

    Consider upping your warnings to max.
    Last edited by Elysia; 01-05-2008 at 02:27 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.

  14. #14
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    Made all changes noted above, and its still not decending into the while loop, so the pointers must be screwing up somewhere, but i dont understand why.

    Each time i create a new pointer, assign it to the original file pointer, so as far as i understand it, thats two separate pointers pointing to the same original file location.

    Then i take the copy, and mess around with it, so the original should still be the same?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    You are correct in your understanding.
    However, you still haven't fixed what Salem and others pointed out.
    I might also add that this does NOT apply to files. The FILE* value returned is not for you to modify or duplicate. If you do, then the result when doing file operations again on one pointer, then the next, is undefined.
    Last edited by Elysia; 01-05-2008 at 03:02 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.

Page 1 of 4 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