English 中文(简体)
回归价值是部分的,不是全部的
原标题:Returned value is partial and not full
  • 时间:2012-05-09 12:57:35
  •  标签:
  • c++

I have a file.txt where I m reading each line and I wan t to handle those lines. The file contains IP, Nicknames and some values. I want to save only the IP addresses to another file, but before that I m checking the result returned by my function (char* get_ip(char arr[])).
The problem is the returned value, it s showing me only a partial, e.g:

normal IP address: 66.55.44.33
My return: 66.55.44

Edit:

2项职能:main()get_ip()。

//<----------- FUNCTION get_ip() -------------------- ><br />

    char* get_ip(char buff[]){

    char line[32];

    for(int i = 0; i < sizeof(buff); i++){
        if(buff[i] ==  . ){
            if(isdigit(buff[i + 1])){
                i = 0;
                while(buff[i] !=    ){
                    line[i] = buff[i];
                    i++;
                }
                break;
            }
        }
    }

    if(isdigit(line[0]))
        return line;
    else
        return 0;
}

//<------------ FUNCTION int main() --------------------->


    int main(){
    // variable, opening folder etc.

    char buff[64], *line;

    while(!feof(fph)){
        fgets(buff, 63, fph);
        line = get_ip(buff);

        if(line)
            cout << line << "
";
    }

    } // main() func. end
最佳回答

你们应当展示更多的法典:你签名至少可以发挥作用。

您正在分拨<条码>buff,然后交还。

But arrays are never returned by value, they are decayed to pointer-to-first-element. That means, that when you use your function like this:

char[32] myFunction(...);

char ip[32] = myFunction(...);

页: 1 阵列的起始点为阵列(line)号,在排出范围后予以销毁。

这意味着,它含有垃圾,而且你很幸运,你甚至部分地从中获利(尽管如果是完全的垃圾,你可能更容易追踪问题)。

可能的补救办法是使用<代码>std:string (我建议采用)或将分位数移至myFunction。 (C类解决办法):

char[32] ip;

myFunction(ip, ...);
问题回答

Current expected behavior is not defined, as line is a local variable, you are not allowed to return from the function. If you want it to be separate buffer from buff you should use malloc instead of the declaration char line[32];

一个问题可能是:

for(int i = 0; i < sizeof(buff); i++){

具体地说,声明

sizeof(buff)

虽然你本可以回去64个,即缓冲面积,但你却忘记,C阵列几乎总是向点人倾斜,因此实际上将4个(如果是32个轨道)或8个(如果是64个轨道),即<条码>的范围。

你们需要明确地通过规模。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签