Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 27th, 2005, 2:17 PM   #1
Marek
Newbie
 
Marek's Avatar
 
Join Date: Dec 2005
Location: Poland
Posts: 3
Rep Power: 0 Marek is on a distinguished road
Send a message via MSN to Marek
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!
Marek is offline   Reply With Quote
Old Dec 27th, 2005, 3:19 PM   #2
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
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.
sykkn is offline   Reply With Quote
Old Dec 27th, 2005, 3:26 PM   #3
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Marek
        f1=fopen("C:\\1.txt","r");
        if((f1=fopen("C:\\1.txt","r")) == NULL)
        printf("Error: File doesn't exist");
I can't find your problem, although this one may be the error. You are opening f1 twice. Check f1 using either this code:
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.
Polyphemus_ is offline   Reply With Quote
Old Dec 27th, 2005, 8:40 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Dec 27th, 2005, 9:32 PM   #5
theproject
Newbie
 
Join Date: Dec 2005
Posts: 6
Rep Power: 0 theproject is on a distinguished road
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(){...}

}
anyway, you can make your parsing of the file using the fscanf function, ( if you are in a *nix environment type man fscanf, if not, google it or something.)
Another thing not said is that it is good programing that you close the file once you're done with it ( using fclose ).


Cya []
theproject is offline   Reply With Quote
Old Dec 28th, 2005, 4:14 AM   #6
Marek
Newbie
 
Marek's Avatar
 
Join Date: Dec 2005
Location: Poland
Posts: 3
Rep Power: 0 Marek is on a distinguished road
Send a message via MSN to Marek
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
Marek is offline   Reply With Quote
Old Dec 28th, 2005, 4:43 AM   #7
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
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.
InfoGeek is offline   Reply With Quote
Old Dec 28th, 2005, 5:52 AM   #8
Marek
Newbie
 
Marek's Avatar
 
Join Date: Dec 2005
Location: Poland
Posts: 3
Rep Power: 0 Marek is on a distinguished road
Send a message via MSN to Marek
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)
Marek is offline   Reply With Quote
Old Dec 28th, 2005, 6:59 AM   #9
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by Marek
I was really trying many combinations to solve that.
Voodoo programming won't help. First chalk out a roadmap for your program on the paper then try to implement that in the coding.
Quote:
Originally Posted by Marek
I rewrite this program again...
Post the latest code so we can see it and point out the mistakes.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Dec 28th, 2005, 10:03 AM   #10
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
Quote:
Originally Posted by Marek
This word (author1) has less characters than spaces in array (20), and the "free" spaces are filled by unnecesarry characters.
This is only in response that particular sentence ... you are getting the extra space because I was just setting the very last item in the array to the NULL character. That is not what you really want to do. You want to terminate the string with the null character ... place it just behind the last character.

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.
sykkn is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC