GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Jul-2006, 02:30
whatever84 whatever84 is offline
New Member
 
Join Date: Jul 2006
Posts: 4
whatever84 is on a distinguished road

Beginner problem :(


Hi,
I've spent a lot of time thinking of this problem, I am beginner in C. Now, I decided to discuss this with you and I hope you give me suggestions:

I want to store the chars to buffer, and when it reachs zero byte it prints what it has stored sofar, and up-date the buffer to hold another new found.

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

int main()
{

        FILE* fp;
        char ch;
        char buf[300];
        int i,e;
        e = 20;
        fp = fopen("D:\\MyFolder","rb");

        if(fp==NULL)
                printf("The file dose not eists\n");

        fseek(fp,e,SEEK_SET);
        while((ch=getc(fp))!=EOF){
                if(ch!=0){
                        buf[n]=ch;
                        n++;}
                else{
                        printf("%s\n",buf);//to print the first found
                        //??????????????????????? //I want to update the buffer to hold another new found
                        e+=200;
                        fseek(fp,e,SEEK_SET);
                        }
        }

        fclose(fp);
return 0;

}
Last edited by LuciWiz : 05-Jul-2006 at 02:31. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 05-Jul-2006, 03:33
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Beginner problem :(


CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

int main()
{

        FILE* fp;
        char ch;
        char buf[300];
        int i,e;
        e = 20;
        fp = fopen("D:\\MyFolder","rb");

        if(fp==NULL)
                printf("The file dose not eists\n");
// Problem #1 -- if you get an error opening the file, you
// process the file anyway

        fseek(fp,e,SEEK_SET);
// What is the fseek() for?

        while((ch=getc(fp))!=EOF){
// use more whitespace:  while ((ch = getc(fp)) != EOF){
// Makes the code easier to read

                if(ch!=0){
                        buf[n]=ch;
// You never initialized, nor defined, n.  
// I'll bet this never even compiled, did it?

                        n++;}
                else{

// You never loaded the 0 into the buffer. The printf() 
// doesn't know when to stop printing
                        printf("%s\n",buf);//to print the first found
                        //?????????????//I want to update the buffer to hold another new found
// So set the buffer pointer (n) back to the beginning

                        e+=200;
                        fseek(fp,e,SEEK_SET);
                        }
        }

        fclose(fp);
return 0;

}
Better read the Guidelines. It will help with posting a good question.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 13:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 11:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 08:03
String problem vaha C Programming Language 3 24-May-2005 19:21
Problem with different results every time!! edward_fraser C Programming Language 3 03-May-2005 23:00

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:52.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.