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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:32:00+00:00 2026-05-29T22:32:00+00:00

FILE *f; char buffer[201]; f=fopen(file.txt,r); if (f==NULL) { printf(file doesn’t exist?!\n);return 1; } while(1)

  • 0
FILE *f;

char buffer[201];   
f=fopen("file.txt","r");

if (f==NULL)
{ 
    printf("file doesn't exist?!\n");return 1;
}

while(1)
{
    if ((fgets(buffer, 200, f) == NULL)) 
        break;

    instruction = validatehex(buffer);
    if(instruction == - 1) 
        continue;

    PC += 4;
    Decode(instruction,verbose);
}

The above code gets the input from the file line by line validating it.
The problem is in the validatehex function it always prints “Not correct” even if the value is correct. This code works 100% perfect in windows put not in linux(Ubuntu).

uint32_t validatehex(char input[])
{
    char hexchars[] = "1234567890abcdefABCDEF";
    uint32_t hexvalue = 0;
    char last;
    if((strlen(input) != strspn (input,hexchars)) && ((strlen(input)-1 != strspn (input,hexchars)) &&  input[8] != '\0'))
    {
        printf("NO CORRECT\n"); 
        return INERR;
    }

    sscanf(input,"%08x",&hexvalue);
    return hexvalue;
}

I’ve tried it across to windows with mingw32 c compiler and it works perfect.
The file it reads from just consists of hex values which are 8 digits long on each line.

Can anyone see where the code is going wrong? Or why it is working differently within Linux?

  • 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-29T22:32:01+00:00Added an answer on May 29, 2026 at 10:32 pm

    Linux and Windows use different end-of-line markers: Windows uses \r\n while Linux uses \n. What kind of linebreaks does your file use?

    If you are reading a file written in windows on Linux, you will get an extra \r at the end of the string.

    This is one way to avoid the problem:

    int len = strlen(input);
    if (input[len-1] == '\r') input[len-1] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

FILE *out=fopen64(text.txt,w+); unsigned int write; char *outbuf=new char[write]; //fill outbuf printf(%i\n,ftello64(out)); fwrite(outbuf,sizeof(char),write,out); printf(%i\n,write); printf(%i\n,ftello64(out));
I got the following code: char buffer[2047]; int charsRead; do { if(fscanf(file, %2047[^\n]%n%*c, buffer,
I've got a char* buffer to hold a file that i read in binary
How can I write only every third item in a char buffer to file
I have this code that works fine: FILE *fp; fp = fopen(filename.c_str(), rb); char
You can specify one buffer for your file stream like that: char buf[BUFFER_SIZE]; std::ofstream
int main() { FILE *read_fp; char buffer[BUFSIZ + 1]; int chars_read; memset(buffer, '\0', sizeof(buffer));
I am reading a binary file as: const size_t stBuffer = 256; char buffer[stBuffer];
I have this code: while (1) { char buffer[4096]; memset(buffer,0,4096); int bytes_read = recv(client_fd,
I'm writing to a text file using the following declaration: void create_out_file(char file_name[],long double

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.