Please help... I am getting a weird error

This is a discussion on Please help... I am getting a weird error within the C Programming forums, part of the General Programming Boards category; Code: int main() { int index, points[6]; char *input, *output; FILE *ifp; printf("Which file would you like to read from?\n"); ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    23

    Please help... I am getting a weird error

    Code:
    int main()
    {
    	int index, points[6];
    	char *input, *output;
       FILE *ifp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%s", input);
    	 
    	printf("Which file would you like to write too?\n");
    	scanf("%s", output);
    	
    	//Opens the output file to be written too
    	if ((ifp = fopen(output, "w")) == NULL) 
    	{ 
    		return 0; 
    	} 
    	
    	//Opens the input file too be read from
    	if ((ifp = fopen(input, "r")) == NULL) 
    	{ 
    		return 0; 
    	}
    	 
    
    	return 0;
    	
    }

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Please any advice??? It is due tonight

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Input and Output don't point to anything.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Your input and output do not point to memory you can write to. Use arrays instead of dangling pointers. Then, ditch scanf:
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Im sorry I am real new to pointers. Can you please explain too me how I would point to something and why? Thank you in advance for the help.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Sorry did'nt see you posted a FAQ. I will read that over.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    I really did'nt understand the FAQ at all. I cleaned up the code a bit...

    Code:
    int main()
    {
    	int index, points[6];
    	char *input, *output;
       FILE *ifp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%s", input);
    	 
    	printf("Which file would you like to write too?\n");
    	scanf("%s", output);
    	
    	//Opens the output file to be written too
    	ifp = fopen(output, "w");	
    	//Opens the input file too be read from
    	ifp = fopen(input, "r");
    	
    	
    
    	return 0;
    	
    }
    Can someone just please add the 1 line of code where it points to something. I am sooooo clueless with this. I have been researching this for the last 2 hours and still dont understand it.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Look into arrays, you allready have one named points. Hint you can pass an array to something which expects a pointer.
    If your assignment says that you have to use pointers then use malloc() and free()

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    I think I might have got it... Does this make any sense?

    Code:
    int main()
    {
    	int index, points[6];
    	char *input, *output, a, b;
       FILE *ifp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%s", &a);
    	input = &a;
    	
    	printf("Which file would you like to write too?\n");
    	scanf("%s", &b);
    	output = &b;
    	
    	
    	//Opens the output file to be written too
    	ifp = fopen(output, "w");	
    	//Opens the input file too be read from
    	ifp = fopen(input, "r");
    	
    	
    
    	return 0;
    	
    }

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You're getting there but a and b are single chars.
    Code:
    int main()
    {
    	int index, points[6];
    	char input[250], output[250];
       FILE *ifp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%s", input);
    	
    printf("Which file would you like to write too?\n");
    	scanf("%s", output);
    	
    	//Opens the output file to be written too
    	ifp = fopen(output, "w");	
    	//Opens the input file too be read from
    	ifp = fopen(input, "r");
    	
    	
    
    	return 0;
    	
    }
    EDIT: forgot to remove the & which isn't required for arrays.
    Last edited by Quantum1024; 11-15-2005 at 03:03 PM.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    No, you are using the same pointer to open two files.

    Code:
         FILE *ifp = NULL;
         FILE *ifp2 = NULL;
    
         ifp = fopen("Filename.extension","mode_character");
         ifp2 = fopen("Filename2.extension"."mode_character");

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    Thanks so much, now that part works. You will hear more from me in other threads. I still have a lot to go haha

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    23
    fixed...
    Code:
    int main()
    {
    	int index, points[6];
    	char input[250], output[250];
       FILE *ifp, *ofp;
    	
    	printf("Which file would you like to read from?\n");
    	scanf("%s", input);
    
    	
    	printf("Which file would you like to write too?\n");
    	scanf("%s", output);
    	
    	
    	
    	//Opens the output file to be written too
    	ifp = fopen(output, "w");	
    	//Opens the input file too be read from
    	ofp = fopen(input, "r");
    	
    	
    
    	return 0;
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM

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