Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:56:32+00:00 2026-06-14T15:56:32+00:00

I need to store in a text file two integers and then the lines

  • 0

I need to store in a text file two integers and then the lines of a text. i’ve successfully done it by writing each int in a line and each line of the text in a new line too. In order to read it, however, I’ve found some troubles. I’m doing this:

FILE *f = fopen(arquivo, "r");
char *lna = NULL;
fscanf(f, "%d\n%d\n", &maxCol, &maxLin);
//↑This reads the two ints, works fine in step-by-step
for (;;) {
    fscanf(f, "%s\n", &lna);
    //↑This sets lna to NULL always, even if there are more lines
    if (lna != NULL)
        lna[strlen(lna) - 1] = '\0';
    if (feof(f))
        break;
    inserirApos(lista, lna, &atual);
}
fclose(f);

I’ve tryied a few different ways, but they never worked. I understand I can read everthing like strings, with gets or something, but I think that has a problem if the string contains spaces. I wanted to know if the way I’m doing is the best, and what’s wrong with it. I’ve found one of these methods (that didn’t work either) that you have to pass the maximum length of each line. I know this information if necessary, it’s the maxCol I read before.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:56:33+00:00Added an answer on June 14, 2026 at 3:56 pm
    fscanf(f, "%s\n", &lna);
    

    Is the wrong argument type. The %s format expects a char* as argument, but you gave it a char**. And you have not allocated memory to that pointer. fscanf expects a char* pointing to a large enough memory area.

    char *lna = malloc(whatever_you_need);
    ...
        fscanf("%s ", lna);
    

    (no difference between the '\n' and ' ' in the fscanf format. both consume the entire whitespace following the string of non-whitespace characters scanned int lna.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using two string vectors to store two text files. I need to compare
I need to store some data in a text file. The data is in
I'm quite new to SQLite databases and need help using an external text file
Looking forward to help...I need to read two strings from text files and store
I have a directory path stored in a text file, and I need to
An inexperienced question: I need to store about 10 unknown-length text fields per record
I need to find a way to store 250 KB of plain text numbers
Greetings, I need to multiply two extremely long integer values stored in a text
I've got these two structs that I need to store in files. I can't
I've got these two structs that I need to store in files. I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.