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

  • SEARCH
  • Home
  • 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 8892097
In Process

The Archive Base Latest Questions

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

I’m trying to read multiple files. The format of the files would something similar

  • 0

I’m trying to read multiple files. The format of the files would something similar toYYYY-mm-dd-HH-MM.txt and for example 2012-11-26-18-50.txt

I have the following code

char text_buffer[1000];
char file_buffer[256];
int year, month, day, hour, minute;
year = 2012; month = 11; day = 26; hour = 18; minute = 0;

sprintf(file_buffer, "%d-%d-%d-%d-%d.txt", year, month,day,hour,minute);
FILE *ptr_file;
ptr_file=fopen(file_buffer, "r");
if(ptr_file != NULL)
    printf("File opened %s for reading.\n", file_buffer);
else
    printf("Couldn't open %s.\n", file_buffer);

line_number = 0;
while(fgets(buffer,sizeof(buffer), ptr_file) != NULL){
    if(strcmp(buffer, "")==0)
       return 0;
    char *views = strok(buffer, ",");
...
}

I’m assuming that’s all the information needed. I’ll post all of it if it’s still unclear as to where my problem is.
When I run the program. I get both
Filed opened... and Couldn't open.... then a segmentation fault.

Can someone help me figure this out?

Thanks.

  • 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-14T22:54:33+00:00Added an answer on June 14, 2026 at 10:54 pm

    First you will want to make sure you do something (maybe return an error code) when you cannot open the file. A segmentation fault will occur when trying to read from a NULL FILE*.

    Also, you are corrupting memory since file_buffer is pointer, not a buffer. You need to initialize it to something. Lastly, you want to make sure you close the file you open.

    I am also assuming you define buffer somewhere. In your code example I don’t see the definition. Maybe you meant to use text_buffer instead?

    If you are working with LINUX for these types of memory corruptions I would suggest two tools.

    1. GDB, to step through the program a line at time and inspect variables as you go along.
    2. Valgrind, which will let you know about simple memory corruptions like this one.

    These two tools, once you are proficient with them, can help eliminate most simple programming errors.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have thousands of HTML files to process using Groovy/Java and I need to
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.