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 7442543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:11:23+00:00 2026-05-29T11:11:23+00:00

#include <stdio.h> int main() { FILE *f; unsigned int num[80]; int i=0; int rv;

  • 0
#include <stdio.h>

int main() {
FILE *f;
unsigned int num[80];
int i=0;
int rv;
int num_values;

f=fopen("values.txt","r");
if (f==NULL){
    printf("file doesnt exist?!\n");
    return 1;
}

while (i < 80) {
    rv = fscanf(f, "%x", &num[i]);

    if (rv != 1)
        break;

    i++;
}
fclose(f);
num_values = i;

if (i >= 80)
{
    printf("Warning: Stopped reading input due to input too long.\n");
}
else if (rv != EOF)
{
    printf("Warning: Stopped reading input due to bad value.\n");
}
else
{
    printf("Reached end of input.\n");
}

printf("Successfully read %d values:\n", num_values);
for (i = 0; i < num_values; i++)
{
    printf("\t%x\n", num[i]);
}

return 0

}

Got the code above from another question, but having probelms editing it to suit my requirements, everytime i change it seems to error everywhere.

Its only a simple change aswell.

Example input from the file would be
12345678

87654321

1234567811

12345678

When reading in from the file using this method take tthe whole of every line when i only want it to take the first 8 hex numbers and save them.

I tryed using a fgets to get the line and than scanf to format it but the errors just get out of hand.

The idea i had in my head but just not sure how to implement is
1. Open file

  1. fgets current line

  2. scanf to format current line

    • Getting first 8 digits

    • Saving them to the array

  3. loop till end of file (rather than <80 like it is in the code above)

Im a newbee to c and to used to java.

  • 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-05-29T11:11:24+00:00Added an answer on May 29, 2026 at 11:11 am

    Try

    char buffer[201];
    while (fgets(buffer, 200, f) != NULL)
    {
      if (strlen(buffer) > 7)
      {
         buffer[8] = 0;
         sscanf(buffer, "%x", &num[i++]);
      }
      else
      {
         /* report that the line is error ?! */
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> int main() { unsigned char data[1]; FILE *f = fopen(bill.jpg, rb); while
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
#include<stdio.h> int main() { printf(He %c llo,65); } Output: He A llo #include<stdio.h> int
#include<stdio.h> int main() { int a,b; a=a+b; printf(%d,a); return 0; } what should be
#include<stdio.h> void function(int); int main() { int x; printf(Enter x:); scanf(%d, &x); function(x); return
see my code is #include <stdio.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char**argv) { unsigned
#include < stdio.h > #include < string.h > int main() { unsigned char a;

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.