Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

file read operation question

Status
Not open for further replies.

sagar474

Full Member level 5
Joined
Oct 18, 2009
Messages
285
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,318
Location
India,kakinada
Activity points
3,122
is there any better way to do this
I need to read a line of chars separated by space in a file.

Code:
while(1)
    {
        c=fgetc(fp);
        if(c==EOF)break;
        if(c!=' ')
        {
            a[k]=c-48;
            if(c=='X' )a[k]=10;
            else if(c=='/')a[k]+=48;
            else if(c=='\n' ||feof(fp))
            {
                a[k]=11;
                break;
            }
            ++k;
        }

    }
 

Use strtok() for example


#include <string.h>
...
char *token;
char *line = "LINE TO BE SEPARATED";
char *search = " ";


/* Token will point to "LINE". */
token = strtok(line, search);


/* Token will point to "TO". */
token = strtok(NULL, search);


enjoy
 

If you're in windows .net (c, c#, vb), this dotnet function will do it for you, it'll put all the characters in a string[] array
string s = "h e l l o ";
string[] sSplit = s.Split(" ");

Neal

---------- Post added at 08:54 ---------- Previous post was at 08:52 ----------

and of course, to read the file into s
s = System.IO.File.ReadAllText("c:\neals.txt");
 

but I need a fastest way to do this in c,
my program works on Linux platform.
 

strtok() work on C. (linux plataform)

What do you save in a[k]?

What is the format of the file?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top