C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 25th, 2004, 09:46 PM
Maquiavel Maquiavel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 6 Maquiavel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 11 sec
Reputation Power: 0
Weird error with C streams

Hello,
I've read the chapter about C streams today and so I wrote a code to test it.
It compiles well.. but when I run it, Windows shows a message that the program must be closed.
The code:
Code:
#include<stdio.h>
#include<string.h>
int main()
{
	FILE *fp;
	char *str;
 
/* I already have tried:
str = "C Programming...\nLearning C Programming\n";
 
AND 
str = (char*)malloc(180 * sizeof(char));
str = "C Programming...\nLearning C Programming\n";
And now this:*/
	strcpy(str, "C Programming...\nLearning C Programming\n");
// But the error persists
 
	if(!(fp = fopen("strings.txt", "w+")))
	{
		puts("Error while opening the file");
		getchar();
		return 0;
	}
 
	fputs(str, fp);
	rewind(fp);
 
	while(!feof(fp))
	{
		fgets(str, 80, fp);
		printf("%s", str);
	}	
 
	return 0;	
} 


Please, can you compile this code and see if it generates error to you too?
Thanks,
Alfred

Reply With Quote
  #2  
Old August 26th, 2004, 06:09 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
I'm not near a compiler to find out whats causing the actual error but can tell you that the method for setting what str equals is the final method you tried -

Code:
str = (char*)malloc(180 * sizeof(char));
strcpy(str, "C Programming...\nLearning C Programming\n");


If you haven't got the first of these 2 lines then the program will crash since strcpy does *NOT* allocate memory itself.

Hope this helps,

-KM-

Reply With Quote
  #3  
Old August 26th, 2004, 12:16 PM
Maquiavel Maquiavel is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 6 Maquiavel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 11 sec
Reputation Power: 0
Thanks kode_monkey for your reply, I have found a solution:
Now all the things are clear. I was testing the code on DevC++. So, now I tested it on VS .NET, it had compiled and the error was generated, hence I started debugging. The file "fgets.c" that contains the source code for the function fgets has been opened. What it really does is change the string char by char by this code:
if ((*pointer++ = (_TSCHAR)ch) == _T('\n'))

The same as:
char name[10] = "C Programming";
/* Changing */
name[0] = 'A';
name[1] = 'L';


Ok, no problem until here.
char* str = "C Programming"; is a pointer to a CONST string. We can change to where this pointer point to, but we can't change this string. And was this I was trying to change. The function fegts was trying this: str[0] = 'B'; str[1] = 'A'; ...
If fgets would do: str = "New string" it would work, because it would only changing where str points to.
Cya.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Weird error with C streams


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT