C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 17th, 2012, 02:23 PM
Rsk08 Rsk08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 Rsk08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 56 sec
Reputation Power: 0
Trouble reading file with dynamic allocation, can someone help?

hi everyone im doing a program the converts roman numbers into numerical numbers and i´m trying to read from a file some roman numbers and then converts then and show them on the console heres the part of the program where the problem resides:
Code:
void main() {
      if (f != NULL) {
                         
       NRomano1 = (char**) malloc (51 * sizeof(char*));
       Arabe1 = (int*) malloc (51 * sizeof(int));
 
 
        while (!feof(f)) {        
            for (i=0; i<51; i++) 
                NRomano1[i] = (char*) malloc(51 * sizeof(char));
            for (i=0; i<51; i++) {
                fgets(NRomano1[i], 51, f);
                 Arabe1[i] = teclado(NRomano1[i]); /*teclado is the function the converts the numbers*/
                             
             }
                     
             }
            for (i=0; i<51; i++) {
                  printf("O numero romano %s = %d em arabe\n", NRomano1[i], Arabe1[i]);
            }
        fclose(f);
}

its doesnt show debug errors its starts fine, and now the program will allocat 51 spaces i dont want to do that, i wanted that the program asks me how many numbers i want to convert and then goes to the file and reads the exact number i told him to but i cant get it right i´ve been trying to solve this for a long time the problem happens after openning the file, can somehelp me?

Reply With Quote
  #2  
Old June 17th, 2012, 02:34 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,654 kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 2 Months 2 Weeks 2 Days 5 h 1 m 44 sec
Reputation Power: 3436
Use fgets and sscanf to read from STDIN what amount of numbers the user wants to convert and store it in a variable. Then replace your constant 51 with that variable.

If you still have problems, post the full code of what you attempted.
__________________
Recycle your old CD's, don't just trash them


Spidermonkey Tutorial;

If I helped out out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

Reply With Quote
  #3  
Old June 17th, 2012, 02:54 PM
Rsk08 Rsk08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 Rsk08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 56 sec
Reputation Power: 0
Quote:
Originally Posted by kicken
Use fgets and sscanf to read from STDIN what amount of numbers the user wants to convert and store it in a variable. Then replace your constant 51 with that variable.

If you still have problems, post the full code of what you attempted.


i'm a beginner so i dont know how to use sscanf, can you tell me how?

i tryed without the sscanf and when i input the number os numbers i want to convert its just stops there and pauses for a while and then closes, heres the code with the variables and all to make it more clearer.

Code:
				FILE *f;
				int  ne1, *Arabe1, i;
				char **NRomano1;
				char drive[10], ficheiro[20];
				
					system("cls");
					printf("Qual a drive?\n");
					fflush(stdin);
					gets(drive);
					strcat(drive,":\\");
					printf("Qual o nome do ficheiro?\n");
					fflush(stdin);
					gets(ficheiro);
					strcat(drive, ficheiro);
					f= fopen(drive, "r");
					printf("\n\n");
					
					printf("Quantos numeros romanos quer converter?: ");
					scanf("%d", &ne1);
					if (f != NULL) {
						
							NRomano1 = (char**) malloc (ne1 * sizeof(char*));
							Arabe1 = (int*) malloc (ne1 * sizeof(int));

						while (!feof(f)) {		

							for (i=0; i<ne1; i++) 
								NRomano1[i] = (char*) malloc(ne1 * sizeof(char));

						
							for (i=0; i<ne1; i++) {

								fgets(NRomano1[i], ne1, f);
								Arabe1[i] = teclado(NRomano1[i]);
							
							}
					
						}
						for (i=0; i<ne1; i++) {
							printf("O numero romano %s = %d em arabe\n", NRomano1[i], Arabe1[i]);
						}

					}
					else {
						puts("Nao e possivel abrir o ficheiro");
					}
							
					fclose(f);

Reply With Quote
  #4  
Old June 17th, 2012, 08:47 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : June 17th, 2012 at 08:50 PM. Reason: Insert a ` ' character.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Trouble reading file with dynamic allocation, can someone help?

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap