![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Problem with file and array
Hello! I am learning C language, and I have (I am sure "stupid" for experts) problem. I have file what contents looks like this:
author1;title1;year_of_issue1 i would like to read this data, and at first put string "author1" in array a1, so i do something like this: #include <stdio.h>
int main()
{
char a1[20];
int m;
FILE *f1;
f1=fopen("C:\\1.txt","r");
if((f1=fopen("C:\\1.txt","r")) == NULL)
printf("Error: File doesn't exist");
for(m = 0; m < 20; m++)
{
a1[m] = fgetc(f1);
if((fgetc(f1)) == 59)
m = 20;
}
printf("%s",a1);
getchar();
return 0;
}So I am checking if FILE f1 exist, and later i declerate a loop, I would like to stop the loop if i = 59 (becouse 59 is a semicolon ";" in ASCII) and I would like only to have in this array string author1, and in my file after author1 I have ;. So to end the loop m must be equal 20. But it didn't work! As a result i have something like this: "ato14 @" so it is string of silly characters! |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
I can tell you that when your test is calling fgetc() it is dropping every other character ... that's why you ended up with only 'ato' from 'author' ...
for(m = 0; m < 20; m++)
{
if((a1[m] = fgetc(f1)) == 59)
m = 20;
}
/* you'll wanna make sure you string is terminated as well */
a1[19] = "\0";
/* something like that */hopefullly that'll get you moving in the right direction ...
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ Last edited by sykkn; Dec 27th, 2005 at 3:54 PM. |
|
|
|
|
|
#3 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
f1 = fopen("C:\\1.txt", "rb");
if(f1 == NULL)
printf("Error: File doesn't exist");or if((f1 = fopen("C:\\1.txt", "rb")) == NULL)
printf("Error: File doesn't exist");Note I added a b in "rb". This tells the OS to read the file as a binary file, which is safer. One other thing: instead of 59 you can also use ';' (include the quotation marks). When working on bigger project, this will help you when debugging. |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
A few additional comments;
1) Opening a file twice is not a good idea, but can succeed on some systems. 2) Instead of comparing to ASCII 59, use the character ';' to compare with a semi-colon. It makes it a bit more obvious what is happening (few programmers are anally retentive enough to remember the complete set of ASCII character values off by heart) and will also work with non-ASCII character sets. 3) The zero termination of strings, noted by sykkn, is required as printf() keeps putting out characters until it finds a zero byte. 4) Under MS-DOS and windows, it is often better to use a forward slash rather than a backslash. The systems support both forms, but backslash is a special case in C string literals. i.e. change the name "C:\\1.txt" to "C:/1.txt" For paths entered by a user, it doesn't matter: no special handling is needed for either the back or forward slashes. 5) You may also want to add a check for end of line (compare the input against '\n' and EOF) so your code doesn't run rampant if the input is not exactly in the form you expect. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2005
Posts: 6
Rep Power: 0
![]() |
hi there,
first of all you wont want to read the file if it is not open, so you'll want to put the for in the else clause of the if... something like this: if( (f1=fopen("C:\\1.txt","r")) == NULL ){
printf("error....\n");
}else{
for(){...}
}Another thing not said is that it is good programing that you close the file once you're done with it ( using fclose ). Cya [] |
|
|
|
|
|
#6 |
|
Newbie
|
OK, thank you very much. With sykkn solution program is working properly. But there is another problem
Now i would like to do this as well with other words form file (title1;year_of_issue1) So I done something like this:#include <stdio.h>
int main()
{
char a1[20], t1[20], yoi[20];
int m;
FILE *f1;
f1=fopen("C:\\1.txt","r");
if((f1=fopen("C:\\1.txt","r")) == NULL)
printf("Error: File doesn't exist");
for(m = 0; m < 20; m++)
{
if((a1[m] = fgetc(f1)) == 59)
{
for(m = 0; m < 20; m++)
{
if((t1[m] = fgetc(f1)) == 59)
{
for(m = 0; m < 20; m++)
{
if((yoi[m] = fgetc(f1)) == 59)
m = 20;
}
}
}
}
}
printf("%s %s %s",a1, t1, yoi);
getchar();
return 0;
}And this is not working... I have still a lot of stupid characters ![]() |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
There are many problems with your code:
1) You don't need nested loops for what you're trying to achieve. 2) Use separate variables for inner loops if you do intend to work with nested loops(in other programs). 3) You're looping for only 20 characters, loop until an EOF character is encountered or you are unable to read from the file. 4) Please pay attention to what others are saying. 5) First make a design for what are you trying to achieve, only then proceed to coding. Looks like you've just copied & pasted the loops.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#8 |
|
Newbie
|
Yes sorry, bu I really would like to focus on my problem (put this all data on arrays) And after this corect those less important mistakes. I am not experienced enought to write something like this. I thought that my idea (using one variable "m" and inner loops will work corectly...
I am learning C in school. But I have only lectures (zero practice). And lectures are made for people what were lerning C (or C++) earlier. And for students what are starting programming its really hard to uderstand anything. We have to do at home 1 program. This operation what I would like to do here is only a small part of my project but very important. I have no idea why something sometimes is working properly and sometimes not. I rewrite this program again, with evry advices from here, and It is not worlong properly. Earlier with sykkn help it was ok, but now not I thinkt that problem is in that: This word (author1) has less characters than spaces in array (20), and the "free" spaces are filled by unnecesarry characters. I was really trying many combinations to solve that. To make that after ";" it will stop reading character form file etc. But I failed. Can we define an array with not constant number of items? Because I think it will help if i make this array a1 exactly for my word author1, but i can not do this for all items in my file (because there is an assumption that it canbe infinitely many authors, titles, and every will be diffrent etc) |
|
|
|
|
|
#9 | ||
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
Quote:
__________________
PFO - My daily dose of technology. |
||
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
Quote:
for(m = 0; m < 20; m++)
{
if((a1[m] = fgetc(f1)) == 59)
a1[m] = '\0'; /* replaces the semicolon with NULL character */
m = 20;
}That suffers from a flaw whenever you hit the limit of the for loop before hitting the semicolon. At that point you would have a unterminated string. I would probably rewrite it like this ... (assuming we have all of your previous declarations) /* untested ... intended as an example */
m = 0
while( ((a1[m] = fgetc(f1)) != 59) && (m < 19) )
{
m++;
}
a1[m] = '\0';
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ Last edited by sykkn; Dec 28th, 2005 at 10:20 AM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|