2

Here is the COMPLETE text of my program, besides what has been commented out:

#include <stdio.h>



int main (int argc, char* argv[] ){

   FILE* inFile = fopen(argv[1], "r");

   if(inFile==0){
      printf( "Error opening file, terminating program\n");
      return 1;
      }

   char* charArray = malloc(100*sizeof(char));

   int j=0;

   printf("%i", j);


   for(j=0; j++; j<100){
      printf("%c", charArray[j]);
      printf("%c", '\n');
      }


   printf("%i", j);

   return 0;
   }

I will call my program with the parameter text which is the name of a text file I want it to open. The part of my program that is commented out reads the text file into a character array, the same array I am trying to print in the for loop.

Right now, the char array contains complete garbage, but that's not the point--the point is, it's not outputting ANYTHING when it should at least be outputting SOMETHING!

Here is the output I am getting:

01

Somehow, j is being incremented exactly once, but I'm not even getting any endlines printed from within the for loop. What is going on?

4
  • 2
    for(j=0; j<100; j++) Commented Sep 17, 2014 at 4:48
  • It's clear that I have too much on my brain right now. This is such a ridiculous mistake that I'd like to know if it's a good idea to delete the question. I don't think it has much value in helping others. Commented Sep 17, 2014 at 4:50
  • 1
    Actually it helps, especially for the new users, this will teach them to never mess with order of things :) Commented Sep 17, 2014 at 4:57
  • @chouaib I'd vote to close as typo Commented Sep 17, 2014 at 5:15

4 Answers 4

3

Your loop formatting is wrong.

The format for a for loop is:

for(INITIALIZATION; CONDITION; INCREMENT/DECREMENT){

}

You have the CONDITION and INCREMENT/DECREMENT mixed up

Change:

for(j=0; j++; j<100)

To:

for(j=0; j<100; j++)
Sign up to request clarification or add additional context in comments.

Comments

3

change

for(j=0; j++; j<100)

to

for(j=0; j<100; j++)

Comments

3

For your information

Useful syntax for for..loop is

for ( variable initialization; condition; variable update ) {
  Code to execute while the condition is true
}

So change your for..loop to

for(j=0; j<100; j++)

Also why separately print \n using printf("%c", '\n');. It should be enough like

printf("%c\n", charArray[j]);

Comments

1

Replace this

for(j=0; j++; j<100)

With this

for(j=0; j<100; j++)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.