The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Trouble reading file with dynamic allocation, can someone help?
Discuss Trouble reading file with dynamic allocation, can someone help? in the C Programming forum on Dev Shed. Trouble reading file with dynamic allocation, can someone help? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 17th, 2012, 02:23 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 2
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?
|

June 17th, 2012, 02:34 PM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
|
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.
|

June 17th, 2012, 02:54 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 2
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);
|

June 17th, 2012, 08:47 PM
|
 |
Contributing User
|
|
|
|
__________________
[code] Code tags[/code] are essential for python code!
Last edited by b49P23TIvg : June 17th, 2012 at 08:50 PM.
Reason: Insert a ` ' character.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|