Jump to content
Sign in to follow this  
scr4wl

C Read from .txt

Recommended Posts

Hey guys, I'm looking for some quick help.

 

What I'm trying to do is read in from a .txt file. The .txt file has multiple lines of info.

 

I've already got a function that opens the program for reading, but then I need another function (fget_string(FILE* name) ) to read each line of the .txt and store it to the appropriate parts of a structure.

 

Any help would be appreciated, and I can post up code if need be.

 

Thanks everyone!

Share this post


Link to post
Share on other sites

I haven't used C in awhile.

 

I think its something like this:

int numwords; // header in text file that tells you how many words in text file
FILE* wordFile = fopen("filename.txt", "r"); // Open text file to read only
fscanf(wordFile, "%d", &numwords); // Scan in from text file and store an int to variable numwords
fclose(wordFile);

 

I can't really explain it, since I barely remember it. Try this website: http://www.exforsys.com/tutorials/c-language/file-management-in-c.html

Edited by Krazyxazn

Share this post


Link to post
Share on other sites

Yay you might be able to help :)

 

So what you posted I'm ok with.

 

I have this structure:

 

 

Then I need fget_string to read the lines from the file and store it into the structure. <---- I am lost here lol

Share this post


Link to post
Share on other sites

Then I need fget_string to read the lines from the file and store it into the structure. <---- I am lost here lol

Could you upload the text file you're getting input from? Normally when you're required to input data from a text file. There are headers (these tell you how many times you need to read the text file).

 

Take for example my last homework's text file for input was:

5     <--- means there are 5 vertices
3     <--- index of start vertex
7     <--- how many edges; like a header to tell you how many reads you need.
0 1 3     <--- edges 1 - 7 below
0 2 5
1 2 1
1 3 3
2 3 4
2 4 4
3 4 2

1st line: the number of vertices

2nd line: the start vertex index

3rd line: the number of edges listed in this file following this line

4th line onwards: A triple of integers representing an edge <start vertex index, end vertex index, weight>

Only one triple per line.

 

 

So once you know the format of the text file. You can just read it line by line and input it into your struct.

Edited by Krazyxazn

Share this post


Link to post
Share on other sites

This is my text file.

 

I'm unsure of how to actually read in the text file. I believe if I knew that I could just look for the newline characters.

 

EDIT:

 

I was looking around and I found this loop.

   while(fgets(line, 80, fr) != NULL)
  {
 /* get a line, up to 80 chars from fr.  done if NULL */
 sscanf (line, "%ld", &elapsed_seconds);
 /* convert the string to a long int */
 printf ("%ld\n", elapsed_seconds);
  }
  fclose(fr); 

 

EDIT 2:

 

Also the function needs to make a new structure and add it to a linked list every time it takes in a new line. <-- This part I can do if someone helps me understand how you read in from a .txt lol :)

 

Would I be able to make it dynamic rather then a set amount?

exampletext.txt

Share this post


Link to post
Share on other sites

for( i < number of titles) // num of titles = how many are in the text file, in this case 4
{
    fgets(yourFile, "%s", yourStruct.title); // fgets reads a line until newline char

    for ( i < 3 ) // 3 words to skip "number of ninjas:"
         fscanf();
    fscanf(yourFile, "%d", yourStuct.numOfNinjas); // reads int for ninja

    for( i < 2) // skip over Tool Rating:
         fscanf(); 
    fscanf(yourFile, "%d", yourStuct.tools;) // reads in int for tools

    // Continue this for until you finish inputting comments.
    // Then do your create and add to link list at the bottom
}

 

Syntax isn't correct, its just a rough idea of what it would look like.

 

fgets() reads until it hits newLine Character or you run out of space in that char array.

fscanf() reads until you hit a delimiter such as a space character. Can't remember if you need to create a dummy variable to skip over items you don't want or not. It might just work with "fscanf()" alone.

 

When you want to read in a text file without header information. Study the the format of the text file.

Since there are 4 segments you will want to run it in a For loop, 4 times.

Notice that each segment starts with a "Title", which you can read in with fgets().

Then you have words and numbers in the following lines.

Since you don't need the words, you can fscanf() over them until you reach the number. Then you are free to store that number.

Edited by Krazyxazn

Share this post


Link to post
Share on other sites

Syntax isn't correct, its just a rough idea of what it would look like.

 

That awesome, thanks. :)

 

That was the start I was looking for, I'll post back here if I need anymore help.

 

Thanks again!

Share this post


Link to post
Share on other sites

Alright so I need some more help :( (I'm really wishing I learned how to read in from a file before this project :( )

 

So I now understand how to use fgets to store stuff from the file to my structures. The problem is that it doesn't do it dynamically.

 

I need to designate a size: fgets(char* string, int, FILE *)

 

I need it to be able to take in any amount from the file until it reaches the \0 character.

 

Also, i'm still unsure of how to skip through parts of the file so that I can only read the portions I need.

 

Here is an example of how I'm using fgets to put the info in my structures:

 

   entry *new_entry;
               while(myentry->length >= myentry->allocated_size)
               {
                  myentry->allocated_size += 10;
                  myentry->entry_array = (entry *) realloc(myentry->entry_array, sizeof(entry) *myentry->allocated_size);

                  new_entry = &(myentry->entry_array[myentry->length]);
                  fgets(new_entry->title, 1024, in); 

Share this post


Link to post
Share on other sites

Honestly, don't remember C language enough to help you beyond this point. I tried to code it in C and no clue what some of these compile errors mean. It's been too long and I'm really unfamiliar with it now. I would suggestion asking your teacher for help.

Share this post


Link to post
Share on other sites

Honestly, don't remember C language enough to help you beyond this point. I tried to code it in C and no clue what some of these compile errors mean. It's been too long and I'm really unfamiliar with it now. I would suggestion asking your teacher for help.

 

aww, well thanks for the help anyway. :)

Share this post


Link to post
Share on other sites

If you want to go until you get the \0 character (NULL), then just check if fgets returns NULL. That indicates the end of file.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...