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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:48:53+00:00 2026-05-16T07:48:53+00:00

I have the following function: void writeResults(FILE* fp, FILE* fpw, Vector w, int size)

  • 0

I have the following function:

void writeResults(FILE* fp, FILE* fpw, Vector w, int size) {

 Vector x;

 while (1) {

       char line[MAX_DIMENSION];  //max dimension is 200

       if( (fgets(line,MAX_DIMENSION,fp)) == NULL) { //EOF
            return;
       }
       else {
           int i=0;
           while (line[i]!='\0') {
                printf("% d %c\n",i,line[i]); //print to check it
                i++;
           }
       }
 }
}

The line of the file it reads is:

1,1
2,2

However when I print each char until ‘\0’ I get this output:

 0 1  
 1 ,  
 2 1  
 3   
 4   
 0 2  
 1 ,  
 2 2  
 3   
 4   

Does anyone have a clue as to why it reads the extra 3 and 4 chars? (there is no extra spaces in the file).

Note: the file was opened in the following way:

FILE* fp = fopen(fileIn, "r");
if (fp == NULL) {
    perror("Couldn't open File");
    exit(errno);
}
  • 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-16T07:48:54+00:00Added an answer on May 16, 2026 at 7:48 am

    Carriage return, line feed – on Windows?

    It would help if we knew how you opened the file. If you opened it as a text file, then you should not be seeing the two extra characters – just one for the newline. However, if you open it as a binary file, it should indeed read both the CR and the LF.

    If you are on Linux, as indicated in the comments, then we have more diagnostic tools available. Perhaps the simplest to start with is ‘od -c file‘; this will show you exactly what is in the file. Note that if the file was ever on a Windows box, it could still have CRLF line endings. If you use ‘vim’, it might tell you the file type is ‘[dos]’.

    Alternatively, you could print the characters as integers (as well as characters):

    printf("%d (%2d) %c\n", i, line[i], line[i]);
    

    You should see 49 for ‘1’, 50 for ‘2’, 44 for ‘,’, 10 for newline (LF, ‘\n‘), and something else too – that is the mystery (but it would show 13 for CR).


    CR is the \r character in C source. It was used to indicate that the print head should move back to the start of the line (printer carriage returned to start of line); the LF or line feed scrolled the paper up a line. Windows and MS-DOS both use the CRLF (curr-liff) sequence to indicate an end of line; Unix always used just LF, aka newline or NL; MacOS 9 and earlier used just CR.

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

Sidebar

Related Questions

I have a function with the following declaration: void cleanValid(int valid[][4], int &size, int
Suppose I have the following function: void foo(std::vector<int> vec, int n); If I call
I have the following function void initBoard(int * board[BOARD_ROWS][BOARD_COLS]){ int z = 0; for(
Suppose I have a function void myFun(int*) In C++ what exactly does the following
I have something analogous to the following code... void function(int x) { // complicated
suppose we have following function: void someFunction(int * araye){ for (int i=0;i<5;i++) cout <<araye[i]<<'
I have the following function: void Register(Data* _pData, uint32 _Line, const char* _pFile, ...)
I have the following function in C++ void func1() { char *p = Test
Consider I have the following function prototype: void MyFunction(int MyParameter); With the following definition:
I have the following function void DoSomething(int start[10], int end[10]) When I call it

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.