What is wrong with this code?

#include <stdio.h>
int main()
{
FILE *fp;
unsigned long long int b =34;
fp=fopen("g:\\my.txt","w");
while(b<=39) b++;
{

fprintf(fp,"my file %ulld",b );


}
fclose(fp);
return 0;
}
I can't figure out how to make b a ulld and make it output 34-39 in my txt file.
closed account (Dy7SLyTq)
why do u have long twice?. and its a little hard to read a file opened for writing. and i think the function ur looking for is like seekg or something like that
first use code tags to post your code. if you don't know what that is. Its the button that looks like this <> on the right side of the screen, when you are posting. People are being nice enough to take time out of thier day to help you with your code. Using code tages makes it easier for them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
 int main()
 {
 FILE *fp;
 unsigned long long int b =34;
 fp=fopen("g:\\my.txt","w");
 while(b<=39) b++;
 {
 
fprintf(fp,"my file %ulld",b );
 

}
 fclose(fp);
 return 0;
 }


You can clearly see the differance between the two posts. I should probably mention that my c is a little rusty. Anyway, on line 5 what is this? unsigned long long int b =34; I assume it's supposed to be this int b =34; Also take a look at this fp=fopen("g:\\my.txt","w"); is this the right file location? Maybe try the c: drive instead fp=fopen("c:\\my.txt","w");.
closed account (Dy7SLyTq)
no i think its right about the location. its the fact that he opened it up for writing and just used a counter hoping it would start on line 34
fp=fopen("g:\\my.txt","w");

What's the "w" for?
Try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
 int main()
 {
    FILE *fp;
    unsigned long long int b =34;
    fp=fopen("g:\\my.txt","w");
     while(b<=39) 
     {      
        fprintf(fp,"my file %llu\n",b );
        b++;    
    }
    fclose(fp);
    return 0;
 }


@greenleaf800073 The w is for opening the file in write mode.

Edit:
@yanson and DTSCode unsigned long long int is a valid data type and is at least a 64 bit unsigned integer, although it is usually written unsigned long long or uint64_t if you #include <cstdint>
Last edited on
Ah ok, couldn't you guys do this with

#include <fstream>
The OP is writing C.
I'm sorry I did not post as code, I did not know how.
I need it to be unsigned long long int because when I modify it to do what I want it to do it will be using huge numbers much greater than a long int.
the w is for write mode I am trying to write to a file.
I also am having the same trouble with fread when dealing with unsigned long long ints.
closed account (Dy7SLyTq)
why try to print lines 34-39 if your writing to it?
@DTSCode
He's not, I believe he is trying to write the numbers to the file.
closed account (Dy7SLyTq)
ahhhhhhhhhhh.... ur right. i read that wrong (and for some reason thought fprintf was fscanf). nvr mnd
@Kangus: naraku9333 posted the correct code.

In your code:
1
2
3
4
 while(b<=39) b++;
 {
fprintf(fp,"my file %ulld",b );
}


the b++ should be in the same statement block ({}) as the fprintf(), else the while loop completes first, then a single fprintf() call is made with b=40.
Topic archived. No new replies allowed.